home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / host contacted / jikes.lha / jikes-1.11 / src / decl.cpp < prev    next >
C/C++ Source or Header  |  2000-01-06  |  190KB  |  4,608 lines

  1. // $Id: decl.cpp,v 1.40 2000/01/06 08:24:30 lord Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #include "config.h"
  11. #include <sys/stat.h>
  12. #include "semantic.h"
  13. #include "control.h"
  14. #include "depend.h"
  15. #include "table.h"
  16. #include "tuple.h"
  17.  
  18. //
  19. // If this compilation unit contains a package declaration, make sure the package
  20. // is associated with a directory and that the name of the package is not also
  21. // associated with a type.
  22. //
  23. inline void Semantic::CheckPackage()
  24. {
  25.     if (compilation_unit -> package_declaration_opt)
  26.     {
  27.         //
  28.         // Make sure that the package actually exists.
  29.         //
  30.         if (this_package -> directory.Length() == 0 && control.option.directory == NULL)
  31.         {
  32.             ReportSemError(SemanticError::PACKAGE_NOT_FOUND,
  33.                            compilation_unit -> package_declaration_opt -> name -> LeftToken(),
  34.                            compilation_unit -> package_declaration_opt -> name -> RightToken(),
  35.                            this_package -> PackageName());
  36.         }
  37.         else
  38.         {
  39.             //
  40.             // Make sure that the package or any of its parents does not match the name of a type.
  41.             //
  42.             for (PackageSymbol *subpackage = this_package, *package = subpackage -> owner;
  43.                  package;
  44.                  subpackage = package, package = package -> owner)
  45.             {
  46.                 FileSymbol *file_symbol = Control::GetFile(control, package, subpackage -> Identity());
  47.                 if (file_symbol)
  48.                 {
  49.                     char *file_name = file_symbol -> FileName();
  50.                     int length = file_symbol -> FileNameLength();
  51.                     wchar_t *error_name = new wchar_t[length + 1];
  52.                     for (int i = 0; i < length; i++)
  53.                         error_name[i] = file_name[i];
  54.                     error_name[length] = U_NULL;
  55.  
  56.                     ReportSemError(SemanticError::PACKAGE_TYPE_CONFLICT,
  57.                                    compilation_unit -> package_declaration_opt -> name -> LeftToken(),
  58.                                    compilation_unit -> package_declaration_opt -> name -> RightToken(),
  59.                                    package -> PackageName(),
  60.                                    subpackage -> Name(),
  61.                                    error_name);
  62.  
  63.                     delete [] error_name;
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69.     return;
  70. }
  71.  
  72.  
  73. //
  74. // Pass 1: Introduce the main package, the current package and all types specified into their proper scope
  75. //
  76. void Semantic::ProcessTypeNames()
  77. {
  78.     import_on_demand_packages.Next() = control.system_package;
  79.     compilation_unit = source_file_symbol -> compilation_unit;
  80.  
  81.     //
  82.     // If we are supposed to be verbose, report empty declarations...
  83.     //
  84.     if (control.option.pedantic)
  85.     {
  86.         if (compilation_unit -> EmptyCompilationUnitCast())
  87.         {
  88.             ReportSemError(SemanticError::NO_TYPES,
  89.                            compilation_unit -> LeftToken(),
  90.                            compilation_unit -> RightToken());
  91.         }
  92.  
  93.         for (int i = 0; i < compilation_unit -> NumTypeDeclarations(); i++)
  94.         {
  95.             Ast *type_declaration = compilation_unit -> TypeDeclaration(i);
  96.             if (type_declaration -> EmptyDeclarationCast())
  97.             {
  98.                 ReportSemError(SemanticError::EMPTY_DECLARATION,
  99.                                type_declaration -> LeftToken(),
  100.                                type_declaration -> RightToken());
  101.             }
  102.         }
  103.     }
  104.  
  105.     //
  106.     // If we have a bad compilation unit insert its types as "bad types"
  107.     //
  108.     if (compilation_unit -> BadCompilationUnitCast())
  109.     {
  110.         for (int i = 0; i < lex_stream -> NumTypes(); i++)
  111.         {
  112.             LexStream::TokenIndex identifier_token = lex_stream -> Next(lex_stream -> Type(i));
  113.             if (lex_stream -> Kind(identifier_token) == TK_Identifier)
  114.             {
  115.                 NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  116.                 TypeSymbol *type = this_package -> FindTypeSymbol(name_symbol);
  117.  
  118.                 assert(type);
  119.  
  120.                 type -> MarkBad();
  121.                 type -> MarkSourceNoLongerPending();
  122.                 type -> supertypes_closure = new SymbolSet;
  123.                 type -> subtypes = new SymbolSet;
  124.                 type -> semantic_environment = new SemanticEnvironment((Semantic *) this, type, NULL);
  125.                 if (type != control.Object())
  126.                     type -> super = (type == control.Throwable() ? control.Object() : control.Throwable());
  127.                 if (! type -> FindConstructorSymbol())
  128.                     AddDefaultConstructor(type);
  129.                 source_file_symbol -> types.Next() = type;
  130.             }
  131.         }
  132.  
  133.         return;
  134.     }
  135.  
  136.     //
  137.     // Use this tuple to compute the list of valid types encountered in this
  138.     // compilation unit.
  139.     //
  140.     Tuple<TypeSymbol *> type_list;
  141.  
  142.     //
  143.     // Process each type in this compilation unit, in turn
  144.     //
  145.     for (int k = 0; k < compilation_unit -> NumTypeDeclarations(); k++)
  146.     {
  147.         LexStream::TokenIndex identifier_token;
  148.         TypeSymbol *type = NULL;
  149.  
  150.         Ast *type_declaration = compilation_unit -> TypeDeclaration(k);
  151.         switch(type_declaration -> kind)
  152.         {
  153.             case Ast::CLASS:
  154.             {
  155.                 AstClassDeclaration *class_declaration = (AstClassDeclaration *) type_declaration;
  156.                 identifier_token = class_declaration -> identifier_token;
  157.                 NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  158.  
  159.                 type = this_package -> FindTypeSymbol(name_symbol);
  160.                 if (type)
  161.                 {
  162.                     if (! type -> SourcePending())
  163.                     {
  164.                         ReportSemError(SemanticError::DUPLICATE_TYPE_DECLARATION,
  165.                                        identifier_token,
  166.                                        identifier_token,
  167.                                        name_symbol -> Name(),
  168.                                        type -> FileLoc());
  169.                         type = NULL;
  170.                     }
  171.                     else
  172.                     {
  173.                         if (type -> ContainingPackage() == control.unnamed_package)
  174.                         {
  175.                             TypeSymbol *old_type = (TypeSymbol *) control.unnamed_package_types.Image(name_symbol);
  176.                             if (old_type != type)
  177.                             {
  178.                                 ReportSemError(SemanticError::DUPLICATE_TYPE_DECLARATION,
  179.                                                identifier_token,
  180.                                                identifier_token,
  181.                                                name_symbol -> Name(),
  182.                                                old_type -> FileLoc());
  183.                             }
  184.                         }
  185.  
  186.                         type_list.Next() = type; // Save valid type for later processing. See below
  187.  
  188.                         type -> MarkSourceNoLongerPending();
  189.                         type -> semantic_environment = new SemanticEnvironment((Semantic *) this, type, NULL);
  190.                         type -> declaration = class_declaration;
  191.                         type -> SetFlags(ProcessClassModifiers(class_declaration));
  192.                         //
  193.                         // Add 3 extra elements for padding. May need a default constructor and other support elements.
  194.                         //
  195.                         type -> SetSymbolTable(class_declaration -> class_body -> NumClassBodyDeclarations() + 3);
  196.                         type -> SetLocation();
  197.  
  198.                         if (lex_stream -> IsDeprecated(lex_stream -> Previous(class_declaration -> LeftToken())))
  199.                             type -> MarkDeprecated();
  200.  
  201.                         source_file_symbol -> types.Next() = type;
  202.                         class_declaration -> semantic_environment = type -> semantic_environment; // save for processing bodies later.
  203.  
  204.                         CheckClassMembers(type, class_declaration -> class_body);
  205.                     }
  206.                 }
  207.  
  208.                 break;
  209.             }
  210.             case Ast::INTERFACE:
  211.             {
  212.                 AstInterfaceDeclaration *interface_declaration = (AstInterfaceDeclaration *) type_declaration;
  213.                 identifier_token = interface_declaration -> identifier_token;
  214.                 NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  215.  
  216.                 type = this_package -> FindTypeSymbol(name_symbol);
  217.                 if (type)
  218.                 {
  219.                     if (! type -> SourcePending())
  220.                     {
  221.                         ReportSemError(SemanticError::DUPLICATE_TYPE_DECLARATION,
  222.                                        identifier_token,
  223.                                        identifier_token,
  224.                                        name_symbol -> Name(),
  225.                                        type -> FileLoc());
  226.                         type = NULL;
  227.                     }
  228.                     else
  229.                     {
  230.                         if (type -> ContainingPackage() == control.unnamed_package)
  231.                         {
  232.                             TypeSymbol *old_type = (TypeSymbol *) control.unnamed_package_types.Image(name_symbol);
  233.                             if (old_type != type)
  234.                             {
  235.                                 ReportSemError(SemanticError::DUPLICATE_TYPE_DECLARATION,
  236.                                                identifier_token,
  237.                                                identifier_token,
  238.                                                name_symbol -> Name(),
  239.                                                old_type -> FileLoc());
  240.                             }
  241.                         }
  242.  
  243.                         type_list.Next() = type; // Save valid type for later processing. See below
  244.  
  245.                         type -> MarkSourceNoLongerPending();
  246.                         type -> semantic_environment = new SemanticEnvironment((Semantic *) this, type, NULL);
  247.                         type -> declaration = interface_declaration;
  248.                         type -> file_symbol = source_file_symbol;
  249.                         type -> SetFlags(ProcessInterfaceModifiers(interface_declaration));
  250.                         type -> SetSymbolTable(interface_declaration -> NumInterfaceMemberDeclarations());
  251.                         type -> SetLocation();
  252.  
  253.                         if (lex_stream -> IsDeprecated(lex_stream -> Previous(interface_declaration -> LeftToken())))
  254.                             type -> MarkDeprecated();
  255.  
  256.                         source_file_symbol -> types.Next() = type;
  257.                         interface_declaration -> semantic_environment = type -> semantic_environment;
  258.  
  259.                         CheckInterfaceMembers(type, interface_declaration);
  260.                     }
  261.                 }
  262.                 break;
  263.             }
  264.             case Ast::EMPTY_DECLARATION:
  265.                  break;
  266.             default:
  267.                 assert(false);
  268.                 break;
  269.         }
  270.  
  271.         //
  272.         // If we successfully processed this type, check that
  273.         //     . its name does not conflict with a subpackage
  274.         //     . if it is contained in a file with a diffent name
  275.         //       than its own name that there does not also exist a
  276.         //       (java or class) file with its name.
  277.         //
  278.         if (type)
  279.         {
  280.             NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  281.             for (int i = 0; i < this_package -> directory.Length(); i++)
  282.             {
  283.                 if (this_package -> directory[i] -> FindDirectorySymbol(name_symbol))
  284.                 {
  285.                     char *file_name = type -> file_symbol -> FileName();
  286.                     int length = type -> file_symbol -> FileNameLength();
  287.                     wchar_t *error_name = new wchar_t[length + 1];
  288.                     for (int j = 0; j < length; j++)
  289.                         error_name[j] = file_name[j];
  290.                     error_name[length] = U_NULL;
  291.  
  292.                     ReportSemError(SemanticError::PACKAGE_TYPE_CONFLICT,
  293.                                    identifier_token,
  294.                                    identifier_token,
  295.                                    this_package -> PackageName(),
  296.                                    name_symbol -> Name(),
  297.                                    error_name);
  298.  
  299.                     delete [] error_name;
  300.                 }
  301.             }
  302.  
  303.             if (type -> Identity() != source_file_symbol -> Identity())
  304.             {
  305.                 PackageSymbol *package = this_package;
  306.                 FileSymbol *file_symbol = Control::GetJavaFile(package, type -> Identity());
  307.  
  308.                 if (file_symbol)
  309.                 {
  310.                     ReportSemError(SemanticError::TYPE_IN_MULTIPLE_FILES,
  311.                                    identifier_token,
  312.                                    identifier_token,
  313.                                    this_package -> PackageName(),
  314.                                    source_file_symbol -> Name(),
  315.                                    package -> PackageName(),
  316.                                    type -> Name());
  317.                 }
  318.             }
  319.         }
  320.     }
  321.  
  322.     CheckPackage();
  323.     ProcessImports();
  324.  
  325.     //
  326.     // Process outer type of superclasses and interfaces and make sure that compilation unit
  327.     // contains exactly one public type.
  328.     //
  329.     TypeSymbol *public_type = NULL;
  330.     for (int i = 0; i < type_list.Length(); i++)
  331.     {
  332.         TypeSymbol *type = type_list[i];
  333.  
  334.         AstClassDeclaration *class_declaration = type -> declaration -> ClassDeclarationCast();
  335.         AstInterfaceDeclaration *interface_declaration = type -> declaration -> InterfaceDeclarationCast();
  336.         if (class_declaration)
  337.              ProcessSuperTypeDependences(class_declaration);
  338.         else ProcessSuperTypeDependences(interface_declaration);
  339.  
  340.         if (type && type -> ACC_PUBLIC())
  341.         {
  342.             if (! public_type)
  343.             {
  344.                 public_type = type;
  345.  
  346.                 if  (source_file_symbol -> Identity() != public_type -> Identity())
  347.                 {
  348.                     if (class_declaration)
  349.                     {
  350.                         ReportSemError(SemanticError::MISMATCHED_TYPE_AND_FILE_NAMES,
  351.                                        class_declaration -> identifier_token,
  352.                                        class_declaration -> identifier_token,
  353.                                        public_type -> Name());
  354.                     }
  355.                     else
  356.                     {
  357.                         ReportSemError(SemanticError::MISMATCHED_TYPE_AND_FILE_NAMES,
  358.                                        interface_declaration -> identifier_token,
  359.                                        interface_declaration -> identifier_token,
  360.                                        public_type -> Name());
  361.                     }
  362.                 }
  363.             }
  364.             else
  365.             {
  366.                 if (class_declaration)
  367.                 {
  368.                     ReportSemError(SemanticError::MULTIPLE_PUBLIC_TYPES,
  369.                                    class_declaration -> identifier_token,
  370.                                    class_declaration -> identifier_token,
  371.                                    type -> Name(),
  372.                                    public_type -> Name());
  373.                 }
  374.                 else
  375.                 {
  376.                     ReportSemError(SemanticError::MULTIPLE_PUBLIC_TYPES,
  377.                                    interface_declaration -> identifier_token,
  378.                                    interface_declaration -> identifier_token,
  379.                                    type -> Name(),
  380.                                    public_type -> Name());
  381.                 }
  382.             }
  383.         }
  384.     }
  385.  
  386.     return;
  387. }
  388.  
  389.  
  390. void Semantic::CheckClassMembers(TypeSymbol *containing_type, AstClassBody *class_body)
  391. {
  392.     for (int i = 0; i < class_body -> NumNestedClasses(); i++)
  393.     {
  394.         AstClassDeclaration *class_declaration = class_body -> NestedClass(i);
  395.  
  396.         ProcessNestedClassName(containing_type, class_declaration);
  397.     }
  398.  
  399.     for (int j = 0; j < class_body -> NumNestedInterfaces(); j++)
  400.     {
  401.         AstInterfaceDeclaration *interface_declaration = class_body -> NestedInterface(j);
  402.  
  403.         ProcessNestedInterfaceName(containing_type, interface_declaration);
  404.     }
  405.  
  406.     for (int l = 0; l < class_body -> NumEmptyDeclarations(); l++)
  407.     {
  408.         if (control.option.pedantic)
  409.         {
  410.             ReportSemError(SemanticError::EMPTY_DECLARATION,
  411.                            class_body -> EmptyDeclaration(l) -> LeftToken(),
  412.                            class_body -> EmptyDeclaration(l) -> RightToken());
  413.         }
  414.     }
  415.  
  416.     return;
  417. }
  418.  
  419.  
  420. inline TypeSymbol *Semantic::FindTypeInShadow(TypeShadowSymbol *type_shadow_symbol, LexStream::TokenIndex identifier_token)
  421. {
  422.     //
  423.     // Recall that even an inaccessible member x of a super class (or interface) S,
  424.     // in addition to not been inherited by a subclass, hides all other occurrences of x that may
  425.     // appear in a super class (or super interface) of S (see 8.3).
  426.     //
  427.     TypeSymbol *type_symbol = type_shadow_symbol -> type_symbol;
  428.  
  429.     for (int i = 0; i < type_shadow_symbol -> NumConflicts(); i++)
  430.     {
  431.         ReportSemError(SemanticError::AMBIGUOUS_TYPE,
  432.                        identifier_token,
  433.                        identifier_token,
  434.                        type_symbol -> Name(),
  435.                        type_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  436.                        type_symbol -> owner -> TypeCast() -> ExternalName(),
  437.                        type_shadow_symbol -> Conflict(i) -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  438.                        type_shadow_symbol -> Conflict(i) -> owner -> TypeCast() -> ExternalName());
  439.     }
  440.  
  441.     return type_symbol;
  442. }
  443.  
  444.  
  445. //
  446. // Look for a type within an environment stack, without regard to inheritance !!!
  447. //
  448. TypeSymbol *Semantic::FindTypeInEnvironment(SemanticEnvironment *env_stack, NameSymbol *name_symbol)
  449. {
  450.     for (SemanticEnvironment *env = env_stack; env; env = env -> previous)
  451.     {
  452.         TypeSymbol *type = env -> symbol_table.FindTypeSymbol(name_symbol);
  453.         if (type)
  454.             return type;
  455.         type = env -> Type() -> FindTypeSymbol(name_symbol);
  456.         if (type)
  457.             return type;
  458.         if (name_symbol == env -> Type() -> Identity())
  459.             return env -> Type();
  460.     }
  461.  
  462.     return (TypeSymbol *) NULL;
  463. }
  464.  
  465.  
  466. void Semantic::CheckNestedTypeDuplication(SemanticEnvironment *env, LexStream::TokenIndex identifier_token)
  467. {
  468.     NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  469.  
  470.     //
  471.     // First check to see if we have a duplication at the same level...
  472.     //
  473.     TypeSymbol *old_type = (env -> symbol_table.Size() > 0 ? env -> symbol_table.Top() -> FindTypeSymbol(name_symbol)
  474.                                                            : env -> Type() -> FindTypeSymbol(name_symbol));
  475.     if (old_type)
  476.     {
  477.         ReportSemError(SemanticError::DUPLICATE_TYPE_DECLARATION,
  478.                        identifier_token,
  479.                        identifier_token,
  480.                        name_symbol -> Name(),
  481.                        old_type -> FileLoc());
  482.     }
  483.     else
  484.     {
  485.         //
  486.         // ... Then check the enclosing environments...
  487.         //
  488.         for (; env; env = env -> previous)
  489.         {
  490.             if (env -> Type() -> Identity() == name_symbol)
  491.             {
  492.                 ReportSemError(SemanticError::DUPLICATE_INNER_TYPE_NAME,
  493.                                identifier_token,
  494.                                identifier_token,
  495.                                name_symbol -> Name(),
  496.                                env -> Type() -> FileLoc());
  497.                 break;
  498.             }
  499.         }
  500.     }
  501.  
  502.     return;
  503. }
  504.  
  505.  
  506. TypeSymbol *Semantic::ProcessNestedClassName(TypeSymbol *containing_type, AstClassDeclaration *class_declaration)
  507. {
  508.     CheckNestedTypeDuplication(containing_type -> semantic_environment, class_declaration -> identifier_token);
  509.  
  510.     NameSymbol *name_symbol = lex_stream -> NameSymbol(class_declaration -> identifier_token);
  511.     TypeSymbol *outermost_type = containing_type -> outermost_type;
  512.  
  513.     int length = containing_type -> ExternalNameLength() + 1 + name_symbol -> NameLength(); // +1 for $,... +1 for $
  514.     wchar_t *external_name = new wchar_t[length + 1]; // +1 for '\0';
  515.     wcscpy(external_name, containing_type -> ExternalName());
  516.     wcscat(external_name, StringConstant::US__DS);
  517.     wcscat(external_name, name_symbol -> Name());
  518.  
  519.     TypeSymbol *inner_type = containing_type -> InsertNestedTypeSymbol(name_symbol);
  520.     inner_type -> outermost_type = outermost_type;
  521.     inner_type -> supertypes_closure = new SymbolSet;
  522.     inner_type -> subtypes = new SymbolSet;
  523.     inner_type -> SetExternalIdentity(control.FindOrInsertName(external_name, length));
  524.     inner_type -> semantic_environment = new SemanticEnvironment((Semantic *) this,
  525.                                                                  inner_type,
  526.                                                                  containing_type -> semantic_environment);
  527.     inner_type -> declaration = class_declaration;
  528.     inner_type -> file_symbol = source_file_symbol;
  529.     inner_type -> SetFlags(containing_type -> ACC_INTERFACE()
  530.                                             ? ProcessStaticNestedClassModifiers(class_declaration)
  531.                                             : ProcessNestedClassModifiers(class_declaration));
  532.     inner_type -> SetOwner(containing_type);
  533.     //
  534.     // Add 3 extra elements for padding. May need a default constructor and other support elements.
  535.     //
  536.     inner_type -> SetSymbolTable(class_declaration -> class_body -> NumClassBodyDeclarations() + 3);
  537.     inner_type -> SetLocation();
  538.     inner_type -> SetSignature(control);
  539.  
  540.     if (lex_stream -> IsDeprecated(lex_stream -> Previous(class_declaration -> LeftToken())))
  541.         inner_type -> MarkDeprecated();
  542.  
  543.     //
  544.     // If not a top-level type, then add pointer to enclosing type.
  545.     //
  546.     if (! inner_type -> ACC_STATIC())
  547.         inner_type -> InsertThis(0);
  548.  
  549.     if (inner_type -> IsLocal())
  550.     {
  551.         if (! outermost_type -> local)
  552.             outermost_type -> local = new SymbolSet;
  553.         outermost_type -> local -> AddElement(inner_type);
  554.     }
  555.     else
  556.     {
  557.         if (! outermost_type -> non_local)
  558.             outermost_type -> non_local = new SymbolSet;
  559.         outermost_type -> non_local -> AddElement(inner_type);
  560.     }
  561.  
  562.     class_declaration -> semantic_environment = inner_type -> semantic_environment; // save for processing bodies later.
  563.  
  564.     CheckClassMembers(inner_type, class_declaration -> class_body);
  565.  
  566.     delete [] external_name;
  567.  
  568.     return inner_type;
  569. }
  570.  
  571.  
  572. void Semantic::CheckInterfaceMembers(TypeSymbol *containing_type, AstInterfaceDeclaration *interface_declaration)
  573. {
  574.     for (int i = 0; i < interface_declaration -> NumNestedClasses(); i++)
  575.     {
  576.         AstClassDeclaration *class_declaration = interface_declaration -> NestedClass(i);
  577.  
  578.         ProcessNestedClassName(containing_type, class_declaration);
  579.     }
  580.  
  581.     for (int j = 0; j < interface_declaration -> NumNestedInterfaces(); j++)
  582.     {
  583.         AstInterfaceDeclaration *inner_interface_declaration = interface_declaration -> NestedInterface(j);
  584.  
  585.         ProcessNestedInterfaceName(containing_type, inner_interface_declaration);
  586.     }
  587.  
  588.     for (int l = 0; l < interface_declaration -> NumEmptyDeclarations(); l++)
  589.     {
  590.         if (control.option.pedantic)
  591.         {
  592.             ReportSemError(SemanticError::EMPTY_DECLARATION,
  593.                            interface_declaration -> EmptyDeclaration(l) -> LeftToken(),
  594.                            interface_declaration -> EmptyDeclaration(l) -> RightToken());
  595.         }
  596.     }
  597.  
  598.     return;
  599. }
  600.  
  601.  
  602. TypeSymbol *Semantic::ProcessNestedInterfaceName(TypeSymbol *containing_type, AstInterfaceDeclaration *interface_declaration)
  603. {
  604.     CheckNestedTypeDuplication(containing_type -> semantic_environment, interface_declaration -> identifier_token);
  605.  
  606.     NameSymbol *name_symbol = lex_stream -> NameSymbol(interface_declaration -> identifier_token);
  607.     TypeSymbol *outermost_type = containing_type -> outermost_type;
  608.  
  609.     int length = containing_type -> ExternalNameLength() + 1 + name_symbol -> NameLength(); // +1 for $,... +1 for $
  610.     wchar_t *external_name = new wchar_t[length + 1]; // +1 for '\0';
  611.     wcscpy(external_name, containing_type -> ExternalName());
  612.     wcscat(external_name, StringConstant::US__DS);
  613.     wcscat(external_name, name_symbol -> Name());
  614.  
  615.     TypeSymbol *inner_type = containing_type -> InsertNestedTypeSymbol(name_symbol);
  616.     inner_type -> outermost_type = outermost_type;
  617.     inner_type -> supertypes_closure = new SymbolSet;
  618.     inner_type -> subtypes = new SymbolSet;
  619.     inner_type -> SetExternalIdentity(control.FindOrInsertName(external_name, length));
  620.     inner_type -> semantic_environment = new SemanticEnvironment((Semantic *) this,
  621.                                                                  inner_type,
  622.                                                                  containing_type -> semantic_environment);
  623.     inner_type -> declaration = interface_declaration;
  624.     inner_type -> file_symbol = source_file_symbol;
  625.     inner_type -> SetFlags(ProcessNestedInterfaceModifiers(interface_declaration));
  626.     inner_type -> SetOwner(containing_type);
  627.     inner_type -> SetSymbolTable(interface_declaration -> NumInterfaceMemberDeclarations());
  628.     inner_type -> SetLocation();
  629.     inner_type -> SetSignature(control);
  630.  
  631.     if (lex_stream -> IsDeprecated(lex_stream -> Previous(interface_declaration -> LeftToken())))
  632.         inner_type -> MarkDeprecated();
  633.  
  634.     if (inner_type -> IsLocal())
  635.     {
  636.         if (! outermost_type -> local)
  637.             outermost_type -> local = new SymbolSet;
  638.         outermost_type -> local -> AddElement(inner_type);
  639.     }
  640.     else
  641.     {
  642.         if (! outermost_type -> non_local)
  643.             outermost_type -> non_local = new SymbolSet;
  644.         outermost_type -> non_local -> AddElement(inner_type);
  645.     }
  646.  
  647.     interface_declaration -> semantic_environment = inner_type -> semantic_environment; // save for processing bodies later.
  648.  
  649.     CheckInterfaceMembers(inner_type, interface_declaration);
  650.  
  651.     delete [] external_name;
  652.  
  653.     return inner_type;
  654. }
  655.  
  656.  
  657. //
  658. // Pass 1.2: Process all import statements
  659. //
  660. void Semantic::ProcessImports()
  661. {
  662.     for (int i = 0; i < compilation_unit -> NumImportDeclarations(); i++)
  663.     {
  664.         AstImportDeclaration *import_declaration = compilation_unit -> ImportDeclaration(i);
  665.  
  666.         if (import_declaration -> star_token_opt)
  667.              ProcessTypeImportOnDemandDeclaration(import_declaration);
  668.         else ProcessSingleTypeImportDeclaration(import_declaration);
  669.     }
  670.  
  671.     return;
  672. }
  673.  
  674.  
  675. //
  676. // Pass 1.3: Process outer types in "extends" and "implements" clauses associated with the types.
  677. //
  678. void Semantic::ProcessSuperTypeDependences(AstClassDeclaration *class_declaration)
  679. {
  680.     TypeSymbol *type = class_declaration -> semantic_environment -> Type();
  681.     if (class_declaration -> super_opt)
  682.     {
  683.         TypeSymbol *super_type = FindFirstType(class_declaration -> super_opt) -> symbol -> TypeCast();
  684.         if (super_type)
  685.             super_type -> subtypes -> AddElement(type -> outermost_type);
  686.     }
  687.  
  688.     for (int k = 0; k < class_declaration -> NumInterfaces(); k++)
  689.     {
  690.          TypeSymbol *super_type = FindFirstType(class_declaration -> Interface(k)) -> symbol -> TypeCast();
  691.          if (super_type)
  692.          {
  693.              assert(super_type -> subtypes);
  694.  
  695.              super_type -> subtypes -> AddElement(type -> outermost_type);
  696.          }
  697.     }
  698.  
  699.     SetDefaultSuperType(class_declaration);
  700.  
  701.     AstClassBody *class_body = class_declaration -> class_body;
  702.     for (int i = 0; i < class_body -> NumNestedClasses(); i++)
  703.         ProcessSuperTypeDependences(class_body -> NestedClass(i));
  704.  
  705.     for (int j = 0; j < class_body -> NumNestedInterfaces(); j++)
  706.         ProcessSuperTypeDependences(class_body -> NestedInterface(j));
  707.  
  708.     return;
  709. }
  710.  
  711.  
  712. void Semantic::ProcessSuperTypeDependences(AstInterfaceDeclaration *interface_declaration)
  713. {
  714.     TypeSymbol *type = interface_declaration -> semantic_environment -> Type();
  715.     for (int k = 0; k < interface_declaration -> NumExtendsInterfaces(); k++)
  716.     {
  717.         TypeSymbol *super_type = FindFirstType(interface_declaration -> ExtendsInterface(k)) -> symbol -> TypeCast();
  718.         if (super_type)
  719.             super_type -> subtypes -> AddElement(type -> outermost_type);
  720.     }
  721.  
  722.     SetDefaultSuperType(interface_declaration);
  723.  
  724.     for (int i = 0; i < interface_declaration -> NumNestedClasses(); i++)
  725.         ProcessSuperTypeDependences(interface_declaration -> NestedClass(i));
  726.  
  727.     for (int j = 0; j < interface_declaration -> NumNestedInterfaces(); j++)
  728.         ProcessSuperTypeDependences(interface_declaration -> NestedInterface(j));
  729.  
  730.     return;
  731. }
  732.  
  733.  
  734. void Semantic::SetDefaultSuperType(AstClassDeclaration *class_declaration)
  735. {
  736.     TypeSymbol *type = class_declaration -> semantic_environment -> Type();
  737.  
  738.     //
  739.     // If a type has no super type, set it up properly in case
  740.     // it is expanded prematurely by one of its dependents.
  741.     //
  742.     if (! class_declaration -> super_opt && class_declaration -> NumInterfaces() == 0)
  743.     {
  744.         if (type -> Identity() != control.object_name_symbol ||
  745.             type -> ContainingPackage() != control.system_package || type -> IsNested())
  746.             type -> super = control.Object();
  747.     }
  748.  
  749.     return;
  750. }
  751.  
  752.  
  753. void Semantic::SetDefaultSuperType(AstInterfaceDeclaration *interface_declaration)
  754. {
  755.     TypeSymbol *type = interface_declaration -> semantic_environment -> Type();
  756.  
  757.     //
  758.     // Set it up an interface properly in case it is
  759.     // expanded prematurely by one of its dependents.
  760.     //
  761.     type -> super = control.Object();
  762.  
  763.     for (int i = 0; i < interface_declaration -> NumNestedClasses(); i++)
  764.     {
  765.         AstClassDeclaration *inner_class_declaration = interface_declaration -> NestedClass(i);
  766.         if (inner_class_declaration -> semantic_environment)
  767.             SetDefaultSuperType(inner_class_declaration);
  768.     }
  769.  
  770.     for (int j = 0; j < interface_declaration -> NumNestedInterfaces(); j++)
  771.     {
  772.         AstInterfaceDeclaration *inner_interface_declaration = interface_declaration -> NestedInterface(j);
  773.         if (inner_interface_declaration -> semantic_environment)
  774.             SetDefaultSuperType(inner_interface_declaration);
  775.     }
  776.  
  777.     return;
  778. }
  779.  
  780.  
  781. //
  782. // Pass 2: Process "extends" and "implements" clauses associated with the types.
  783. //
  784. void Semantic::ProcessTypeHeader(AstClassDeclaration *class_declaration)
  785. {
  786.     TypeSymbol *this_type = class_declaration -> semantic_environment -> Type();
  787.  
  788.     assert(! this_type -> HeaderProcessed() || this_type -> Bad());
  789.  
  790.     //
  791.     // If the class does not have a super type then ...
  792.     //
  793.     if (! class_declaration -> super_opt)
  794.     {
  795.         if (this_type -> Identity() != control.object_name_symbol ||
  796.             this_package != control.system_package || this_type -> IsNested())
  797.             SetObjectSuperType(this_type, class_declaration -> identifier_token);
  798.     }
  799.     else
  800.     {
  801.         TypeSymbol *super_type = MustFindType(class_declaration -> super_opt);
  802.  
  803.         assert(this_type -> subtypes_closure);
  804.         assert(! super_type -> SourcePending());
  805.  
  806.         this_type -> super = super_type;
  807.  
  808.         if (this_type -> subtypes_closure -> IsElement(super_type)) // if there is a cycle, break it and issue an error message
  809.         {
  810.             this_type -> super = control.Object();
  811.             this_type -> MarkCircular();
  812.             ReportSemError(SemanticError::CIRCULAR_CLASS,
  813.                            class_declaration -> identifier_token,
  814.                            class_declaration -> super_opt -> RightToken(),
  815.                            this_type -> ContainingPackage() -> PackageName(),
  816.                            this_type -> ExternalName());
  817.         }
  818.         else if (this_type -> Identity() == control.object_name_symbol &&
  819.                  this_package == control.system_package && (! this_type -> IsNested()))
  820.         {
  821.              ReportSemError(SemanticError::OBJECT_WITH_SUPER_TYPE,
  822.                             class_declaration -> super_opt -> LeftToken(),
  823.                             class_declaration -> super_opt -> RightToken(),
  824.                             this_type -> ContainingPackage() -> PackageName(),
  825.                             this_type -> ExternalName());
  826.              this_type -> super = NULL;
  827.         }
  828.         else if (this_type -> super -> ACC_INTERFACE())
  829.         {
  830.             ReportSemError(SemanticError::NOT_A_CLASS,
  831.                            class_declaration -> super_opt -> LeftToken(),
  832.                            class_declaration -> super_opt -> RightToken(),
  833.                            this_type -> super -> ContainingPackage() -> PackageName(),
  834.                            this_type -> super -> ExternalName());
  835.  
  836.             SetObjectSuperType(this_type, class_declaration -> identifier_token);
  837.         }
  838.         else if (this_type -> super -> ACC_FINAL())
  839.         {
  840.              ReportSemError(SemanticError::SUPER_IS_FINAL,
  841.                             class_declaration -> super_opt -> LeftToken(),
  842.                             class_declaration -> super_opt -> RightToken(),
  843.                             this_type -> super -> ContainingPackage() -> PackageName(),
  844.                             this_type -> super -> ExternalName());
  845.         }
  846.     }
  847.  
  848.     for (int i = 0; i < class_declaration -> NumInterfaces(); i++)
  849.         ProcessInterface(this_type, class_declaration -> Interface(i));
  850.  
  851.     this_type -> MarkHeaderProcessed();
  852.  
  853.     return;
  854. }
  855.  
  856.  
  857. void Semantic::ProcessTypeHeader(AstInterfaceDeclaration *interface_declaration)
  858. {
  859.     TypeSymbol *this_type = interface_declaration -> semantic_environment -> Type();
  860.  
  861.     assert(! this_type -> HeaderProcessed() || this_type -> Bad());
  862.  
  863.     SetObjectSuperType(this_type, interface_declaration -> identifier_token);
  864.     for (int k = 0; k < interface_declaration -> NumExtendsInterfaces(); k++)
  865.         ProcessInterface(this_type, interface_declaration -> ExtendsInterface(k));
  866.  
  867.     assert(this_type -> subtypes_closure);
  868.  
  869.     for (int i = 0; i < this_type -> NumInterfaces(); i++)
  870.     {
  871.         if (this_type -> subtypes_closure -> IsElement(this_type -> Interface(i)))
  872.         {
  873.             this_type -> ResetInterfaces(); // Remove all the interfaces if a loop is detected. The error will be reported later
  874.             this_type -> MarkCircular();
  875.             ReportSemError(SemanticError::CIRCULAR_INTERFACE,
  876.                            interface_declaration -> identifier_token,
  877.                            interface_declaration -> ExtendsInterface(interface_declaration -> NumExtendsInterfaces() - 1) -> RightToken(),
  878.                            this_type -> ContainingPackage() -> PackageName(),
  879.                            this_type -> ExternalName());
  880.             break;
  881.         }
  882.     }
  883.  
  884.     this_type -> MarkHeaderProcessed();
  885.  
  886.     return;
  887. }
  888.  
  889.  
  890. //
  891. // Marked type and all other types that are nested inside it "circular"
  892. //
  893. void Semantic::MarkCircularNest(TypeSymbol *type)
  894. {
  895.     if (type -> Circular())
  896.         return;
  897.  
  898.     //
  899.     // Mark the type as circular
  900.     //
  901.     type -> MarkCircular();
  902.     type -> super = control.Object();
  903.     type -> ResetInterfaces();
  904.  
  905.     //
  906.     // Recursively, process any nested type...
  907.     //
  908.     AstClassDeclaration *class_declaration = type -> declaration -> ClassDeclarationCast();
  909.     if (class_declaration)
  910.     {
  911.         AstClassBody *class_body = class_declaration -> class_body;
  912.         for (int i = 0; i < class_body -> NumNestedClasses(); i++)
  913.             MarkCircularNest(class_body -> NestedClass(i) -> semantic_environment -> Type());
  914.         for (int k = 0; k < class_body -> NumNestedInterfaces(); k++)
  915.             MarkCircularNest(class_body -> NestedInterface(k) -> semantic_environment -> Type());
  916.     }
  917.     else
  918.     {
  919.         AstInterfaceDeclaration *interface_declaration = (AstInterfaceDeclaration *) type -> declaration;
  920.         for (int i = 0; i < interface_declaration -> NumNestedClasses(); i++)
  921.             MarkCircularNest(interface_declaration -> NestedClass(i) -> semantic_environment -> Type());
  922.         for (int k = 0; k < interface_declaration -> NumNestedInterfaces(); k++)
  923.             MarkCircularNest(interface_declaration -> NestedInterface(k) -> semantic_environment -> Type());
  924.     }
  925.  
  926.     return;
  927. }
  928.  
  929.  
  930. //
  931. // Compute the set of super types associated with this outer-level type
  932. // and check for circularity.
  933. //
  934. void Semantic::ProcessSuperTypesOfOuterType(TypeSymbol *type)
  935. {
  936.     assert((! type -> IsNested()) || type -> owner -> MethodCast());
  937.  
  938.     if (type -> super)
  939.     {
  940.         type -> supertypes_closure -> AddElement(type -> super -> outermost_type);
  941.         type -> supertypes_closure -> Union(*type -> super -> outermost_type -> supertypes_closure);
  942.     }
  943.  
  944.     for (int k = 0; k < type -> NumInterfaces(); k++)
  945.     {
  946.         type -> supertypes_closure -> AddElement(type -> Interface(k) -> outermost_type);
  947.         type -> supertypes_closure -> Union(*type -> Interface(k) -> outermost_type -> supertypes_closure);
  948.     }
  949.  
  950.     SymbolSet &inner_types = *(type -> innertypes_closure);
  951.     for (TypeSymbol *inner_type = (TypeSymbol *) inner_types.FirstElement();
  952.                      inner_type;
  953.                      inner_type = (TypeSymbol *) inner_types.NextElement())
  954.     {
  955.         TypeSymbol *super_type = inner_type -> super;
  956.         for (int k = 0; super_type;
  957.                         super_type = (TypeSymbol *) (k < inner_type -> NumInterfaces() ? inner_type -> Interface(k++) : NULL))
  958.         {
  959.             if (super_type -> outermost_type != type)
  960.             {
  961.                 type -> supertypes_closure -> AddElement(super_type -> outermost_type);
  962.                 type -> supertypes_closure -> Union(*super_type -> outermost_type -> supertypes_closure);
  963.             }
  964.         }
  965.     }
  966.  
  967.     bool circular = type -> supertypes_closure -> IsElement(type) ||
  968.                     type -> subtypes_closure -> Intersects(*type -> supertypes_closure);
  969.     if (circular)
  970.     {
  971.         if (type -> Circular())        // If the type is already marked circular, an error message has already been issued
  972.             type -> MarkNonCircular(); // Remove the circular mark, so that we can remark the whole "nest" ?
  973.         else
  974.         {
  975.             if (type -> ACC_INTERFACE())
  976.             {
  977.                 AstInterfaceDeclaration *interface_declaration = (AstInterfaceDeclaration *) type -> declaration;
  978.                 int right_token_index = interface_declaration -> NumExtendsInterfaces() - 1;
  979.  
  980.                 ReportSemError(SemanticError::CIRCULAR_INTERFACE,
  981.                                interface_declaration -> identifier_token,
  982.                                (interface_declaration -> NumExtendsInterfaces() > 0
  983.                                                        ? interface_declaration -> ExtendsInterface(right_token_index) -> RightToken()
  984.                                                        : interface_declaration -> identifier_token),
  985.                                type -> ContainingPackage() -> PackageName(),
  986.                                type -> ExternalName());
  987.             }
  988.             else
  989.             {
  990.                 AstClassDeclaration *class_declaration = (AstClassDeclaration *) type -> declaration;
  991.                 int right_token_index = class_declaration -> NumInterfaces() - 1;
  992.  
  993.                 ReportSemError(SemanticError::CIRCULAR_CLASS,
  994.                                class_declaration -> identifier_token,
  995.                                (class_declaration -> NumInterfaces() > 0
  996.                                                    ? class_declaration -> Interface(right_token_index) -> RightToken()
  997.                                                    : (class_declaration -> super_opt
  998.                                                                          ? class_declaration -> super_opt -> RightToken()
  999.                                                                          : class_declaration -> identifier_token)),
  1000.                                type -> ContainingPackage() -> PackageName(),
  1001.                                type -> ExternalName());
  1002.  
  1003.                 SetObjectSuperType(type, class_declaration -> identifier_token);
  1004.  
  1005.                 assert(type -> Identity() != control.object_name_symbol || type -> ContainingPackage() != control.system_package);
  1006.             }
  1007.         }
  1008.  
  1009.         MarkCircularNest(type);
  1010.     }
  1011.  
  1012.     return;
  1013. }
  1014.  
  1015.  
  1016. //
  1017. // The array partially_ordered_types contains a list of inner types. For
  1018. // each of these types, compute the set of super types associated with it
  1019. // and check for circularity.
  1020. //
  1021. void Semantic::ProcessSuperTypesOfInnerType(TypeSymbol *type, Tuple<TypeSymbol *> &partially_ordered_types)
  1022. {
  1023.     for (int l = 0; l < partially_ordered_types.Length(); l++)
  1024.     {
  1025.         TypeSymbol *inner_type = partially_ordered_types[l];
  1026.  
  1027.         SymbolSet &nested_types = *(inner_type -> innertypes_closure);
  1028.         nested_types.AddElement(inner_type); // Compute reflexive transitive closure
  1029.         for (TypeSymbol *nested_type = (TypeSymbol *) nested_types.FirstElement();
  1030.                          nested_type;
  1031.                          nested_type = (TypeSymbol *) nested_types.NextElement())
  1032.         {
  1033.             TypeSymbol *super_type = nested_type -> super;
  1034.             for (int k = 0; super_type;
  1035.                             super_type = (TypeSymbol *) (k < nested_type -> NumInterfaces() ? nested_type -> Interface(k++) : NULL))
  1036.             {
  1037.                 for ( ; super_type; super_type = super_type -> owner -> TypeCast())
  1038.                 {
  1039.                     if (type -> innertypes_closure -> IsElement(super_type))
  1040.                         break;
  1041.                 }
  1042.  
  1043.                 if (super_type && super_type != inner_type)
  1044.                 {
  1045.                     inner_type -> supertypes_closure -> AddElement(super_type);
  1046.                     inner_type -> supertypes_closure -> Union(*super_type -> supertypes_closure);
  1047.                 }
  1048.             }
  1049.         }
  1050.  
  1051.         bool circular = inner_type -> supertypes_closure -> IsElement(inner_type) ||
  1052.                         inner_type -> subtypes_closure -> Intersects(*inner_type -> supertypes_closure);
  1053.  
  1054.         if (circular)
  1055.         {
  1056.             MarkCircularNest(inner_type);
  1057.  
  1058.             if (inner_type -> ACC_INTERFACE())
  1059.             {
  1060.                 AstInterfaceDeclaration *interface_declaration = (AstInterfaceDeclaration *) inner_type -> declaration;
  1061.                 ReportSemError(SemanticError::CIRCULAR_INTERFACE,
  1062.                                interface_declaration -> identifier_token,
  1063.                                (interface_declaration -> NumExtendsInterfaces() > 0
  1064.                                                        ? interface_declaration -> ExtendsInterface(interface_declaration -> NumExtendsInterfaces() - 1) -> RightToken()
  1065.                                                        : interface_declaration -> identifier_token),
  1066.                                inner_type -> ContainingPackage() -> PackageName(),
  1067.                                inner_type -> ExternalName());
  1068.             }
  1069.             else
  1070.             {
  1071.                 AstClassDeclaration *class_declaration = (AstClassDeclaration *) inner_type -> declaration;
  1072.                 ReportSemError(SemanticError::CIRCULAR_CLASS,
  1073.                                class_declaration -> identifier_token,
  1074.                                (class_declaration -> NumInterfaces() > 0
  1075.                                                    ? class_declaration -> Interface(class_declaration -> NumInterfaces() - 1) -> RightToken()
  1076.                                                    : (class_declaration -> super_opt
  1077.                                                                          ? class_declaration -> super_opt -> RightToken()
  1078.                                                                          : class_declaration -> identifier_token)),
  1079.                                inner_type -> ContainingPackage() -> PackageName(),
  1080.                                inner_type -> ExternalName());
  1081.             }
  1082.         }
  1083.     }
  1084.  
  1085.     //
  1086.     // At this point the innertypes_closure set contains only the
  1087.     // immediate inner types.
  1088.     //
  1089.     if (partially_ordered_types.Length() > 1) // inner_types set has more than one element?
  1090.     {
  1091.         SymbolSet &inner_types = *(type -> innertypes_closure);
  1092.  
  1093.         assert(partially_ordered_types.Length() == inner_types.Size());
  1094.  
  1095.         TopologicalSort *topological_sorter = new TopologicalSort(inner_types, partially_ordered_types);
  1096.         topological_sorter -> Sort();
  1097.         delete topological_sorter;
  1098.     }
  1099.  
  1100.     //
  1101.     // Now, complete the closure set of inner types.
  1102.     //
  1103.     for (int i = 0; i < partially_ordered_types.Length(); i++)
  1104.     {
  1105.         TypeSymbol *inner_type = partially_ordered_types[i];
  1106.         type -> AddNestedType(inner_type);
  1107.         type -> innertypes_closure -> Union(*(inner_type -> innertypes_closure));
  1108.     }
  1109.  
  1110.     return;
  1111. }
  1112.  
  1113.  
  1114. void Semantic::ProcessTypeHeaders(AstClassDeclaration *class_declaration)
  1115. {
  1116.     TypeSymbol *this_type = class_declaration -> semantic_environment -> Type();
  1117.  
  1118.     assert(state_stack.Size() == 0 || this_type -> owner -> MethodCast()); // Not a nested type or a immediately local type.
  1119.  
  1120.     ProcessTypeHeader(class_declaration);
  1121.     ProcessNestedTypeHeaders(this_type, class_declaration -> class_body);
  1122.     ProcessSuperTypesOfOuterType(this_type);
  1123.  
  1124.     //
  1125.     // Note that if we are processing an outermost type, no environment is set before we
  1126.     // invoke ProcessTypeHeader to process its super types. Therefore, the dependence map
  1127.     // is not updated with the super type information. In that case, we do so here.
  1128.     //
  1129.     if (state_stack.Size() == 0)
  1130.     {
  1131.         if (this_type -> super)
  1132.         {
  1133.             AddDependence(this_type,
  1134.                           this_type -> super,
  1135.                           (class_declaration -> super_opt ? class_declaration -> super_opt -> LeftToken()
  1136.                                                           : class_declaration -> identifier_token));
  1137.              //
  1138.              // Also, check whether or not the super type of this outermost type is accessible.
  1139.              //
  1140.              if (class_declaration -> super_opt)
  1141.              {
  1142.                  state_stack.Push(this_type -> semantic_environment); // we need an environment for the type check.
  1143.                  TypeAccessCheck(class_declaration -> super_opt, this_type -> super);
  1144.                  state_stack.Pop();
  1145.              }
  1146.        }
  1147.  
  1148.         for (int i = 0; i < class_declaration -> NumInterfaces(); i++)
  1149.         {
  1150.             if (class_declaration -> Interface(i) -> Type())
  1151.                 AddDependence(this_type,
  1152.                               class_declaration -> Interface(i) -> Type(),
  1153.                               class_declaration -> Interface(i) -> LeftToken());
  1154.         }
  1155.     }
  1156.  
  1157.     return;
  1158. }
  1159.  
  1160.  
  1161. void Semantic::ProcessTypeHeaders(AstInterfaceDeclaration *interface_declaration)
  1162. {
  1163.     TypeSymbol *this_type = interface_declaration -> semantic_environment -> Type();
  1164.  
  1165.     assert(state_stack.Size() == 0); // Not a nested type
  1166.  
  1167.     ProcessTypeHeader(interface_declaration);
  1168.     ProcessNestedTypeHeaders(interface_declaration);
  1169.     ProcessSuperTypesOfOuterType(interface_declaration -> semantic_environment -> Type());
  1170.  
  1171.     //
  1172.     // Note that no environment is set before we invoke ProcessTypeHeader to process the
  1173.     // super types of this_type. As a result, the dependence map is not updated with tha
  1174.     // information. We do so here.
  1175.     //
  1176.     for (int i = 0; i < interface_declaration -> NumExtendsInterfaces(); i++)
  1177.     {
  1178.         if (interface_declaration -> ExtendsInterface(i) -> Type())
  1179.             AddDependence(this_type,
  1180.                           interface_declaration -> ExtendsInterface(i) -> Type(),
  1181.                           interface_declaration -> ExtendsInterface(i) -> LeftToken());
  1182.     }
  1183.  
  1184.     return;
  1185. }
  1186.  
  1187.  
  1188. void Semantic::ReportTypeInaccessible(LexStream::TokenIndex left_tok, LexStream::TokenIndex right_tok, TypeSymbol *type)
  1189. {
  1190.     ReportSemError(SemanticError::TYPE_NOT_ACCESSIBLE,
  1191.                    left_tok,
  1192.                    right_tok,
  1193.                    type -> ContainingPackage() -> PackageName(),
  1194.                    type -> ExternalName(),
  1195.                    (type -> ACC_PRIVATE() ? StringConstant::US_private : (type -> ACC_PROTECTED() ? StringConstant::US_protected : StringConstant::US_default)));
  1196.  
  1197.     return;
  1198. }
  1199.  
  1200.  
  1201. TypeSymbol *Semantic::FindNestedType(TypeSymbol *type, LexStream::TokenIndex identifier_token)
  1202. {
  1203.     if (type == control.null_type || type == control.no_type || type -> Primitive())
  1204.         return NULL;
  1205.  
  1206.     NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  1207.  
  1208.     if (! type -> expanded_type_table)
  1209.         ComputeTypesClosure(type, identifier_token);
  1210.     TypeShadowSymbol *type_shadow_symbol = type -> expanded_type_table -> FindTypeShadowSymbol(name_symbol);
  1211.  
  1212.     return (type_shadow_symbol ? FindTypeInShadow(type_shadow_symbol, identifier_token)
  1213.                                : type -> FindTypeSymbol(name_symbol));
  1214. }
  1215.  
  1216.  
  1217. TypeSymbol *Semantic::MustFindNestedType(TypeSymbol *type, Ast *name)
  1218. {
  1219.     AstSimpleName *simple_name = name -> SimpleNameCast();
  1220.     LexStream::TokenIndex identifier_token = (simple_name ? simple_name -> identifier_token
  1221.                                                           : ((AstFieldAccess *) name) -> identifier_token);
  1222.  
  1223.     TypeSymbol *inner_type = FindNestedType(type, identifier_token);
  1224.     if (inner_type)
  1225.          TypeAccessCheck(name, inner_type);
  1226.     else inner_type = GetBadNestedType(type, identifier_token);
  1227.  
  1228.     return inner_type;
  1229. }
  1230.  
  1231.  
  1232. //
  1233. // The Ast name is a qualified name (simple name or a field access). The function FindTypeInLayer
  1234. // searches for the first subname that is the name of a type contained in the set inner_types.
  1235. // If such a type is found, it is returned. Otherwise, the whole qualified name is resolved to
  1236. // a symbol that is returned.
  1237. //
  1238. TypeSymbol *Semantic::FindTypeInLayer(Ast *name, SymbolSet &inner_types)
  1239. {
  1240.     //
  1241.     // Unwind all the field accesses until we get to a base that is a simple name
  1242.     //
  1243.     Tuple<AstFieldAccess *> field;
  1244.     for (AstFieldAccess *field_access = name -> FieldAccessCast(); field_access; field_access = field_access -> base -> FieldAccessCast())
  1245.     {
  1246.         field.Next() = field_access;
  1247.         name = field_access -> base;
  1248.     }
  1249.  
  1250.     //
  1251.     // If the simple_name base is a type that is an element in the inner_types set
  1252.     // return it. Otherwise, assume it is a package name...
  1253.     //
  1254.     AstSimpleName *simple_name = name -> SimpleNameCast();
  1255.  
  1256.     assert(simple_name);
  1257.  
  1258.     PackageSymbol *package = NULL;
  1259.     TypeSymbol *type = FindType(simple_name -> identifier_token);
  1260.     if (type)
  1261.     {
  1262.         if (inner_types.IsElement(type))
  1263.             return type;
  1264.     }
  1265.     else // If the simple_name is not a type, assume it is a package
  1266.     {
  1267.         NameSymbol *name_symbol = lex_stream -> NameSymbol(simple_name -> identifier_token);
  1268.         package = control.external_table.FindPackageSymbol(name_symbol);
  1269.         if (! package)
  1270.             package = control.external_table.InsertPackageSymbol(name_symbol, NULL);
  1271.         control.FindPathsToDirectory(package);
  1272.     }
  1273.  
  1274.     //
  1275.     // We now go through the field access in order until we either encouter a type that is an element of inner_types,
  1276.     // in which case, we return the type. Otherwise, we return NULL.
  1277.     //
  1278.     //
  1279.     for (int i = field.Length() - 1; i >= 0; i--)
  1280.     {
  1281.         AstFieldAccess *field_access = field[i];
  1282.  
  1283.         if (type) // The base name is a type that is not contained in the inner_types set?
  1284.         {
  1285.             type = FindNestedType(type, field_access -> identifier_token); // resolve the next type...
  1286.             if (! type)
  1287.                 break;
  1288.             if (inner_types.IsElement(type))
  1289.                 return type;
  1290.         }
  1291.         else
  1292.         {
  1293.             NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  1294.             type = package -> FindTypeSymbol(name_symbol);
  1295.             if (! type)
  1296.             {
  1297.                 FileSymbol *file_symbol = Control::GetFile(control, package, name_symbol);
  1298.                 if (file_symbol)
  1299.                     type = ReadType(file_symbol, package, name_symbol, field_access -> identifier_token);
  1300.             }
  1301.             else if (type -> SourcePending())
  1302.                  control.ProcessHeaders(type -> file_symbol);
  1303.  
  1304.             //
  1305.             //
  1306.             //
  1307.             if (type)
  1308.             {
  1309.                 if (inner_types.IsElement(type))
  1310.                     return type;
  1311.             }
  1312.             else // If the field access was not resolved to a type assume it is a package
  1313.             {
  1314.                 NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  1315.                 PackageSymbol *subpackage = package -> FindPackageSymbol(name_symbol);
  1316.                 if (! subpackage)
  1317.                     subpackage = package -> InsertPackageSymbol(name_symbol);
  1318.                 control.FindPathsToDirectory(subpackage);
  1319.                 package = subpackage;
  1320.             }
  1321.         }
  1322.     }
  1323.  
  1324.     return NULL;
  1325. }
  1326.  
  1327.  
  1328. void Semantic::ProcessNestedSuperTypes(TypeSymbol *type)
  1329. {
  1330.     int num_inner_types = type -> innertypes_closure -> Size();
  1331.  
  1332.     if (num_inner_types > 0)
  1333.     {
  1334.         SymbolSet &inner_types = *(type -> innertypes_closure);
  1335.  
  1336.         for (TypeSymbol *inner_type = (TypeSymbol *) inner_types.FirstElement();
  1337.                          inner_type;
  1338.                          inner_type = (TypeSymbol *) inner_types.NextElement())
  1339.         {
  1340.             if (inner_type -> ACC_INTERFACE())
  1341.             {
  1342.                 AstInterfaceDeclaration *inner_interface_declaration = (AstInterfaceDeclaration *) inner_type -> declaration;
  1343.  
  1344.                 for (int l = 0; l < inner_interface_declaration -> NumExtendsInterfaces(); l++)
  1345.                 {
  1346.                     AstExpression *interface_name = inner_interface_declaration -> ExtendsInterface(l);
  1347.                     TypeSymbol *super_type = FindTypeInLayer(interface_name, inner_types);
  1348.                     if (super_type)
  1349.                         super_type -> subtypes -> AddElement(inner_type);
  1350.                 }
  1351.             }
  1352.             else
  1353.             {
  1354.                 AstClassDeclaration *inner_class_declaration = (AstClassDeclaration *) inner_type -> declaration;
  1355.  
  1356.                 if (inner_class_declaration -> super_opt)
  1357.                 {
  1358.                     TypeSymbol *super_type = FindTypeInLayer(inner_class_declaration -> super_opt, inner_types);
  1359.                     if (super_type)
  1360.                         super_type -> subtypes -> AddElement(inner_type);
  1361.                 }
  1362.  
  1363.                 for (int l = 0; l < inner_class_declaration -> NumInterfaces(); l++)
  1364.                 {
  1365.                     TypeSymbol *super_type = FindTypeInLayer(inner_class_declaration -> Interface(l), inner_types);
  1366.                     if (super_type)
  1367.                         super_type -> subtypes -> AddElement(inner_type);
  1368.                 }
  1369.             }
  1370.         }
  1371.  
  1372.         //
  1373.         // Create a partial order or the inner types. If there are cycles,
  1374.         // then the order is arbitrary.
  1375.         //
  1376.         Tuple<TypeSymbol *> partially_ordered_types;
  1377.  
  1378.         if (num_inner_types > 0) // inner_types set is not empty?
  1379.         {
  1380.             TypeCycleChecker *cycle_checker = new TypeCycleChecker(partially_ordered_types);
  1381.             cycle_checker -> PartialOrder(inner_types);
  1382.             delete cycle_checker;
  1383.         }
  1384.  
  1385.         for (int k = 0; k < partially_ordered_types.Length(); k++)
  1386.         {
  1387.             TypeSymbol *inner_type = partially_ordered_types[k];
  1388.             if (inner_type -> ACC_INTERFACE())
  1389.             {
  1390.                 AstInterfaceDeclaration *inner_interface_declaration = (AstInterfaceDeclaration *) inner_type -> declaration;
  1391.                 ProcessTypeHeader(inner_interface_declaration);
  1392.                 ProcessNestedTypeHeaders(inner_interface_declaration);
  1393.             }
  1394.             else
  1395.             {
  1396.                 AstClassDeclaration *inner_class_declaration = (AstClassDeclaration *) inner_type -> declaration;
  1397.                 ProcessTypeHeader(inner_class_declaration);
  1398.                 ProcessNestedTypeHeaders(inner_class_declaration -> semantic_environment -> Type(),
  1399.                                         inner_class_declaration -> class_body);
  1400.             }
  1401.         }
  1402.  
  1403.         ProcessSuperTypesOfInnerType(type, partially_ordered_types);
  1404.     }
  1405.  
  1406.     return;
  1407. }
  1408.  
  1409.  
  1410. void Semantic::ProcessNestedTypeHeaders(TypeSymbol *type, AstClassBody *class_body)
  1411. {
  1412.     if (type -> expanded_type_table && (type -> super != control.Object() || type -> NumInterfaces() > 0))
  1413.     {
  1414.         delete type -> expanded_type_table;
  1415.         type  -> expanded_type_table = NULL;
  1416.     }
  1417.  
  1418.     if (! type -> expanded_type_table)
  1419.         ComputeTypesClosure(type, class_body -> left_brace_token);
  1420.  
  1421.     state_stack.Push(type -> semantic_environment);
  1422.  
  1423.     type -> innertypes_closure = new SymbolSet;
  1424.  
  1425.     for (int i = 0; i < class_body -> NumNestedClasses(); i++)
  1426.     {
  1427.         if (class_body -> NestedClass(i) -> semantic_environment)
  1428.             type -> innertypes_closure -> AddElement(class_body -> NestedClass(i) -> semantic_environment -> Type());
  1429.     }
  1430.  
  1431.     for (int j = 0; j < class_body -> NumNestedInterfaces(); j++)
  1432.     {
  1433.         if (class_body -> NestedInterface(j) -> semantic_environment)
  1434.             type -> innertypes_closure -> AddElement(class_body -> NestedInterface(j) -> semantic_environment -> Type());
  1435.     }
  1436.  
  1437.     ProcessNestedSuperTypes(type);
  1438.  
  1439.     state_stack.Pop();
  1440.  
  1441.     return;
  1442. }
  1443.  
  1444.  
  1445. void Semantic::ProcessNestedTypeHeaders(AstInterfaceDeclaration *interface_declaration)
  1446. {
  1447.     TypeSymbol *type = interface_declaration -> semantic_environment -> Type();
  1448.     if (type -> expanded_type_table && type -> NumInterfaces() > 0)
  1449.     {
  1450.         delete type -> expanded_type_table;
  1451.         type  -> expanded_type_table = NULL;
  1452.     }
  1453.  
  1454.     if (! type -> expanded_type_table)
  1455.         ComputeTypesClosure(type, interface_declaration -> identifier_token);
  1456.  
  1457.     state_stack.Push(interface_declaration -> semantic_environment);
  1458.  
  1459.     type -> innertypes_closure = new SymbolSet;
  1460.  
  1461.     for (int i = 0; i < interface_declaration -> NumNestedClasses(); i++)
  1462.     {
  1463.         if (interface_declaration -> NestedClass(i) -> semantic_environment)
  1464.             type -> innertypes_closure -> AddElement(interface_declaration -> NestedClass(i) -> semantic_environment -> Type());
  1465.     }
  1466.  
  1467.     for (int j = 0; j < interface_declaration -> NumNestedInterfaces(); j++)
  1468.     {
  1469.         if (interface_declaration -> NestedInterface(j) -> semantic_environment)
  1470.             type -> innertypes_closure -> AddElement(interface_declaration -> NestedInterface(j) -> semantic_environment -> Type());
  1471.     }
  1472.  
  1473.     ProcessNestedSuperTypes(type);
  1474.  
  1475.     state_stack.Pop();
  1476.  
  1477.     return;
  1478. }
  1479.  
  1480.  
  1481. //
  1482. // Pass 3: Process all method and constructor declarations within the compilation unit so that
  1483. //         any field initialization enclosed in the compilation unit can invoke any constructor or
  1484. //         method within the unit.
  1485. //
  1486. inline void Semantic::ProcessConstructorMembers(AstClassBody *class_body)
  1487. {
  1488.     TypeSymbol *this_type = ThisType();
  1489.  
  1490.     assert(this_type -> HeaderProcessed());
  1491.  
  1492.     //
  1493.     // If the class contains no constructor, ...
  1494.     //
  1495.     if (class_body -> NumConstructors() > 0)
  1496.     {
  1497.         for (int k = 0; k < class_body -> NumConstructors(); k++)
  1498.             ProcessConstructorDeclaration(class_body -> Constructor(k));
  1499.     }
  1500.     else if (! this_type -> Anonymous())
  1501.          AddDefaultConstructor(this_type);
  1502.  
  1503.     this_type -> MarkConstructorMembersProcessed();
  1504.  
  1505.     return;
  1506. }
  1507.  
  1508.  
  1509. inline void Semantic::ProcessMethodMembers(AstClassBody *class_body)
  1510. {
  1511.     assert(ThisType() -> HeaderProcessed());
  1512.  
  1513.     for (int k = 0; k < class_body -> NumMethods(); k++)
  1514.         ProcessMethodDeclaration(class_body -> Method(k));
  1515.  
  1516.     ThisType() -> MarkMethodMembersProcessed();
  1517.  
  1518.     return;
  1519. }
  1520.  
  1521.  
  1522. inline void Semantic::ProcessFieldMembers(AstClassBody *class_body)
  1523. {
  1524.     assert(ThisType() -> HeaderProcessed());
  1525.  
  1526.     for (int i = 0; i < class_body -> NumInstanceVariables(); i++)
  1527.         ProcessFieldDeclaration(class_body -> InstanceVariable(i));
  1528.  
  1529.     for (int k = 0; k < class_body -> NumClassVariables(); k++)
  1530.         ProcessFieldDeclaration(class_body -> ClassVariable(k));
  1531.  
  1532.     ThisType() -> MarkFieldMembersProcessed();
  1533.  
  1534.     return;
  1535. }
  1536.  
  1537.  
  1538. void Semantic::ProcessMembers(SemanticEnvironment *environment, AstClassBody *class_body)
  1539. {
  1540.     //
  1541.     //
  1542.     //
  1543.     state_stack.Push(environment);
  1544.     TypeSymbol *this_type = ThisType();
  1545.  
  1546.     assert(! this_type -> ConstructorMembersProcessed() || this_type -> Bad());
  1547.     assert(! this_type -> MethodMembersProcessed() || this_type -> Bad());
  1548.     assert(! this_type -> FieldMembersProcessed() || this_type -> Bad());
  1549.  
  1550.     ProcessConstructorMembers(class_body);
  1551.     ProcessMethodMembers(class_body);
  1552.     ProcessFieldMembers(class_body);
  1553.  
  1554.     delete this_type -> innertypes_closure; // save some space !!!
  1555.     this_type -> innertypes_closure = NULL;
  1556.  
  1557.     if (! this_type -> IsTopLevel())
  1558.     {
  1559.         for (int i = 0; i < class_body -> NumStaticInitializers(); i++)
  1560.         {
  1561.              ReportSemError(SemanticError::STATIC_INITIALIZER_IN_INNER_CLASS,
  1562.                             class_body -> StaticInitializer(i) -> LeftToken(),
  1563.                             class_body -> StaticInitializer(i) -> RightToken(),
  1564.                             this_type -> Name(),
  1565.                             this_type -> FileLoc());
  1566.         }
  1567.     }
  1568.  
  1569.     for (int i = 0; i < this_type -> NumNestedTypes(); i++)
  1570.     {
  1571.         TypeSymbol *inner_type = this_type -> NestedType(i);
  1572.  
  1573.         if (inner_type -> ACC_INTERFACE())
  1574.         {
  1575.             AstInterfaceDeclaration *interface_declaration = (AstInterfaceDeclaration *) inner_type -> declaration;
  1576.  
  1577.             ProcessMembers(interface_declaration);
  1578.  
  1579.             if (! this_type -> IsTopLevel())
  1580.             {
  1581.                 //
  1582.                 // TODO: 1.1 assumption
  1583.                 //
  1584.                 // As every field in an interface is static, we presume that all interfaces
  1585.                 // should be treated as static entities
  1586.                 //
  1587.                 if (interface_declaration -> semantic_environment)
  1588.                 {
  1589.                     ReportSemError(SemanticError::STATIC_TYPE_IN_INNER_CLASS,
  1590.                                    interface_declaration -> identifier_token,
  1591.                                    interface_declaration -> identifier_token,
  1592.                                    lex_stream -> NameString(interface_declaration -> identifier_token),
  1593.                                    this_type -> Name(),
  1594.                                    this_type -> FileLoc());
  1595.                 }
  1596.             }
  1597.         }
  1598.         else
  1599.         {
  1600.             AstClassDeclaration *class_declaration = (AstClassDeclaration *) inner_type -> declaration;
  1601.  
  1602.             ProcessMembers(class_declaration -> semantic_environment, class_declaration -> class_body);
  1603.  
  1604.             if (! this_type -> IsTopLevel())
  1605.             {
  1606.                 if (class_declaration -> semantic_environment && class_declaration -> semantic_environment -> Type() -> ACC_STATIC())
  1607.                 {
  1608.                     ReportSemError(SemanticError::STATIC_TYPE_IN_INNER_CLASS,
  1609.                                    class_declaration -> identifier_token,
  1610.                                    class_declaration -> identifier_token,
  1611.                                    lex_stream -> NameString(class_declaration -> identifier_token),
  1612.                                    this_type -> Name(),
  1613.                                    this_type -> FileLoc());
  1614.                 }
  1615.             }
  1616.         }
  1617.     }
  1618.  
  1619.     state_stack.Pop();
  1620.  
  1621.     return;
  1622. }
  1623.  
  1624.  
  1625. inline void Semantic::ProcessMethodMembers(AstInterfaceDeclaration *interface_declaration)
  1626. {
  1627.     assert(ThisType() -> HeaderProcessed());
  1628.  
  1629.     for (int k = 0; k < interface_declaration -> NumMethods(); k++)
  1630.         ProcessMethodDeclaration(interface_declaration -> Method(k));
  1631.  
  1632.     ThisType() -> MarkMethodMembersProcessed();
  1633.  
  1634.     return;
  1635. }
  1636.  
  1637.  
  1638. inline void Semantic::ProcessFieldMembers(AstInterfaceDeclaration *interface_declaration)
  1639. {
  1640.     assert(ThisType() -> HeaderProcessed());
  1641.  
  1642.     for (int k = 0; k < interface_declaration -> NumClassVariables(); k++)
  1643.         ProcessFieldDeclaration(interface_declaration -> ClassVariable(k));
  1644.  
  1645.     ThisType() -> MarkFieldMembersProcessed();
  1646.  
  1647.     return;
  1648. }
  1649.  
  1650.  
  1651. void Semantic::ProcessMembers(AstInterfaceDeclaration *interface_declaration)
  1652. {
  1653.     //
  1654.     //
  1655.     //
  1656.     state_stack.Push(interface_declaration -> semantic_environment);
  1657.     TypeSymbol *this_type = ThisType();
  1658.  
  1659.     assert(! this_type -> MethodMembersProcessed() || this_type -> Bad());
  1660.     assert(! this_type -> FieldMembersProcessed() || this_type -> Bad());
  1661.  
  1662.     ProcessMethodMembers(interface_declaration);
  1663.     ProcessFieldMembers(interface_declaration);
  1664.  
  1665.     delete this_type -> innertypes_closure; // save some space !!!
  1666.     this_type -> innertypes_closure = NULL;
  1667.  
  1668.     for (int i = 0; i < this_type -> NumNestedTypes(); i++)
  1669.     {
  1670.         TypeSymbol *inner_type = this_type -> NestedType(i);
  1671.  
  1672.         if (inner_type -> ACC_INTERFACE())
  1673.         {
  1674.             AstInterfaceDeclaration *interface_declaration = (AstInterfaceDeclaration *) inner_type -> declaration;
  1675.             ProcessMembers(interface_declaration);
  1676.         }
  1677.         else
  1678.         {
  1679.             AstClassDeclaration *class_declaration = (AstClassDeclaration *) inner_type -> declaration;
  1680.             ProcessMembers(class_declaration -> semantic_environment, class_declaration -> class_body);
  1681.         }
  1682.     }
  1683.  
  1684.     state_stack.Pop();
  1685.  
  1686.     return;
  1687. }
  1688.  
  1689.  
  1690. //
  1691. // Pass 4: Process the field declarations at the top level of the types
  1692. //
  1693. void Semantic::CompleteSymbolTable(SemanticEnvironment *environment, LexStream::TokenIndex identifier_token, AstClassBody *class_body)
  1694. {
  1695.     if (compilation_unit -> BadCompilationUnitCast())
  1696.         return;
  1697.  
  1698.     state_stack.Push(environment);
  1699.     TypeSymbol *this_type = ThisType();
  1700.  
  1701.     assert(this_type -> ConstructorMembersProcessed());
  1702.     assert(this_type -> MethodMembersProcessed());
  1703.     assert(this_type -> FieldMembersProcessed());
  1704.  
  1705.     //
  1706.     //
  1707.     //
  1708.     if (! this_type -> expanded_method_table)
  1709.         ComputeMethodsClosure(this_type, identifier_token);
  1710.  
  1711.     ExpandedMethodTable &expanded_table = *(this_type -> expanded_method_table);
  1712.     if (! this_type -> ACC_ABSTRACT())
  1713.     {
  1714.         //
  1715.         // Check that every abstract method that is inherited is overridden.
  1716.         //
  1717.         for (int i = 0; i < expanded_table.symbol_pool.Length(); i++)
  1718.         {
  1719.             MethodSymbol *method = expanded_table.symbol_pool[i] -> method_symbol;
  1720.  
  1721.             if (method -> ACC_ABSTRACT())
  1722.             {
  1723.                 TypeSymbol *containing_type = method -> containing_type;
  1724.                 if (containing_type != this_type)
  1725.                 {
  1726.                     if (! method -> IsTyped())
  1727.                         method -> ProcessMethodSignature((Semantic *) this, identifier_token);
  1728.  
  1729.                     //
  1730.                     // If the method is contained in an abstract type read from a class file,
  1731.                     // then it is possible that the abstract method is just out-of-date and needs
  1732.                     // to be recompiled.
  1733.                     //
  1734.                     ReportSemError((! containing_type -> ACC_INTERFACE()) &&
  1735.                                    (containing_type -> file_symbol && containing_type -> file_symbol -> IsClass())
  1736.                                         ? SemanticError::NON_ABSTRACT_TYPE_INHERITS_ABSTRACT_METHOD_FROM_ABSTRACT_CLASS
  1737.                                         : SemanticError::NON_ABSTRACT_TYPE_INHERITS_ABSTRACT_METHOD,
  1738.                                    identifier_token,
  1739.                                    identifier_token,
  1740.                                    method -> Header(),
  1741.                                    containing_type -> ContainingPackage() -> PackageName(),
  1742.                                    containing_type -> ExternalName(),
  1743.                                    this_type -> ContainingPackage() -> PackageName(),
  1744.                                    this_type -> ExternalName());
  1745.                 }
  1746.             }
  1747.         }
  1748.  
  1749.         //
  1750.         // If the super class of this_type is abstract and it is contained in a
  1751.         // different package, check to see if its members include abstract methods
  1752.         // with default access. If so, we must issue error messages for them also
  1753.         // as they cannot be overridden.
  1754.         //
  1755.         if (this_type != control.Object() && this_type -> super -> ACC_ABSTRACT() &&
  1756.             (this_type -> ContainingPackage() != this_type -> super -> ContainingPackage()))
  1757.         {
  1758.             ExpandedMethodTable &super_expanded_table = *(this_type -> super -> expanded_method_table);
  1759.             for (int i = 0; i < super_expanded_table.symbol_pool.Length(); i++)
  1760.             {
  1761.                 MethodSymbol *method = super_expanded_table.symbol_pool[i] -> method_symbol;
  1762.  
  1763.                 if (method -> ACC_ABSTRACT() &&
  1764.                     (! (method -> ACC_PUBLIC() || method -> ACC_PROTECTED() || method -> ACC_PRIVATE())))
  1765.                 {
  1766.                     TypeSymbol *containing_type = method -> containing_type;
  1767.  
  1768.                     if (! method -> IsTyped())
  1769.                         method -> ProcessMethodSignature((Semantic *) this, identifier_token);
  1770.  
  1771.                     //
  1772.                     // If the method is contained in an abstract type read from a class file,
  1773.                     // then it is possible that the abstract method is just out-of-date and needs
  1774.                     // to be recompiled.
  1775.                     //
  1776.                     ReportSemError(SemanticError::NON_ABSTRACT_TYPE_CANNOT_OVERRIDE_DEFAULT_ABSTRACT_METHOD,
  1777.                                    identifier_token,
  1778.                                    identifier_token,
  1779.                                    method -> Header(),
  1780.                                    containing_type -> ContainingPackage() -> PackageName(),
  1781.                                    containing_type -> ExternalName(),
  1782.                                    this_type -> ContainingPackage() -> PackageName(),
  1783.                                    this_type -> ExternalName());
  1784.                 }
  1785.             }
  1786.         }
  1787.     }
  1788.  
  1789.     for (int i = 0; i < expanded_table.symbol_pool.Length(); i++)
  1790.     {
  1791.         MethodShadowSymbol *method_shadow = expanded_table.symbol_pool[i];
  1792.  
  1793.         if (method_shadow -> NumConflicts() > 0)
  1794.         {
  1795.             MethodSymbol *method = method_shadow -> method_symbol;
  1796.  
  1797.             if (method -> containing_type == this_type)
  1798.             {
  1799.                 AstMethodDeclaration *method_declaration = (AstMethodDeclaration *) method -> method_or_constructor_declaration;
  1800.  
  1801.                 for (int k = 0; k < method_shadow -> NumConflicts(); k++)
  1802.                 {
  1803.                     MethodSymbol *hidden_method = method_shadow -> Conflict(k);
  1804.                     if (method -> containing_type != hidden_method -> containing_type) // the methods are not in the same type
  1805.                         CheckMethodOverride(method_declaration, hidden_method);
  1806.                 }
  1807.             }
  1808.             else
  1809.             {
  1810.                 AstClassDeclaration *class_declaration = (AstClassDeclaration *) this_type -> declaration;
  1811.  
  1812.                 for (int k = 0; k < method_shadow -> NumConflicts(); k++)
  1813.                 {
  1814.                     MethodSymbol *hidden_method = method_shadow -> Conflict(k);
  1815.                     if (method -> containing_type != hidden_method -> containing_type) // the methods are not in the same type
  1816.                         CheckMethodOverride(class_declaration, method, hidden_method);
  1817.                 }
  1818.  
  1819.                 if (! method -> ACC_ABSTRACT())
  1820.                 {
  1821.                     if (method -> ACC_STATIC())
  1822.                     {
  1823.                         if (! method -> IsTyped())
  1824.                             method -> ProcessMethodSignature((Semantic *) this, identifier_token);
  1825.  
  1826.                         if (! method_shadow -> Conflict(0) -> IsTyped())
  1827.                             method_shadow -> Conflict(0) -> ProcessMethodSignature((Semantic *) this, identifier_token);
  1828.  
  1829.                         ReportSemError(SemanticError::STATIC_OVERRIDE_ABSTRACT_EXTERNALLY,
  1830.                                        class_declaration -> identifier_token,
  1831.                                        (class_declaration -> NumInterfaces() > 0
  1832.                                              ? class_declaration -> Interface(class_declaration -> NumInterfaces() - 1) -> RightToken()
  1833.                                              : (class_declaration -> super_opt ? class_declaration -> super_opt -> RightToken()
  1834.                                                                                : class_declaration -> identifier_token)),
  1835.                                        lex_stream -> NameString(class_declaration -> identifier_token),
  1836.                                        method -> Header(),
  1837.                                        method -> containing_type -> ContainingPackage() -> PackageName(),
  1838.                                        method -> containing_type -> ExternalName(),
  1839.                                        method_shadow -> Conflict(0) -> Header(),
  1840.                                        method_shadow -> Conflict(0) -> containing_type -> ContainingPackage() -> PackageName(),
  1841.                                        method_shadow -> Conflict(0) -> containing_type -> ExternalName());
  1842.                     }
  1843.                 }
  1844.             }
  1845.  
  1846.             method_shadow -> RemoveConflicts();
  1847.         }
  1848.     }
  1849.  
  1850.     ProcessStaticInitializers(class_body);
  1851.  
  1852.     ProcessBlockInitializers(class_body);
  1853.  
  1854.     //
  1855.     // Reset the this_variable and this_method may have been set in
  1856.     // ProcessStaticInitializers and/or ProcessBlockInitializers.
  1857.     // Indicate that there is no method being currently compiled
  1858.     // in this environment.
  1859.     //
  1860.     ThisVariable() = NULL;
  1861.     ThisMethod() = NULL;
  1862.  
  1863.     //
  1864.     // Recursively process all inner types
  1865.     //
  1866.     for (int l = 0; l < this_type -> NumNestedTypes(); l++)
  1867.     {
  1868.         TypeSymbol *inner_type = this_type -> NestedType(l);
  1869.         if (inner_type -> ACC_INTERFACE())
  1870.             CompleteSymbolTable((AstInterfaceDeclaration *) inner_type -> declaration);
  1871.         else
  1872.         {
  1873.             AstClassDeclaration *class_declaration = (AstClassDeclaration *) inner_type -> declaration;
  1874.             CompleteSymbolTable(class_declaration -> semantic_environment,
  1875.                                 class_declaration -> identifier_token, class_declaration -> class_body);
  1876.         }
  1877.     }
  1878.  
  1879.     state_stack.Pop();
  1880.  
  1881.     return;
  1882. }
  1883.  
  1884.  
  1885. void Semantic::CompleteSymbolTable(AstInterfaceDeclaration *interface_declaration)
  1886. {
  1887.     if (compilation_unit -> BadCompilationUnitCast())
  1888.         return;
  1889.  
  1890.     state_stack.Push(interface_declaration -> semantic_environment);
  1891.     TypeSymbol *this_type = ThisType();
  1892.  
  1893.     assert(this_type -> MethodMembersProcessed());
  1894.     assert(this_type -> FieldMembersProcessed());
  1895.  
  1896.     //
  1897.     //
  1898.     //
  1899.     if (! this_type -> expanded_method_table)
  1900.         ComputeMethodsClosure(this_type, interface_declaration -> identifier_token);
  1901.  
  1902.     ExpandedMethodTable &expanded_table = *(this_type -> expanded_method_table);
  1903.     for (int i = 0; i < interface_declaration -> NumMethods(); i++)
  1904.     {
  1905.         AstMethodDeclaration *method_declaration = interface_declaration -> Method(i);
  1906.         MethodSymbol *method = method_declaration -> method_symbol;
  1907.  
  1908.         if (method)
  1909.         {
  1910.             MethodShadowSymbol *method_shadow = expanded_table.FindOverloadMethodShadow(method,
  1911.                                                                                         (Semantic *) this,
  1912.                                                                                         interface_declaration -> identifier_token);
  1913.             for (int k = 0; k < method_shadow -> NumConflicts(); k++)
  1914.             {
  1915.                 if (method_shadow -> method_symbol -> Type() != method_shadow -> Conflict(k) -> Type())
  1916.                 {
  1917.                     ReportSemError(SemanticError::MISMATCHED_INHERITED_METHOD,
  1918.                                    method_declaration -> method_declarator -> LeftToken(),
  1919.                                    method_declaration -> method_declarator -> RightToken(),
  1920.                                    method_shadow -> method_symbol -> Header(),
  1921.                                    method_shadow -> Conflict(k) -> Header(),
  1922.                                    method_shadow -> Conflict(k) -> containing_type -> ContainingPackage() -> PackageName(),
  1923.                                    method_shadow -> Conflict(k) -> containing_type -> ExternalName());
  1924.                 }
  1925.  
  1926.                 if (method_shadow -> method_symbol -> containing_type == this_type) // override ?
  1927.                     CheckInheritedMethodThrows(method_declaration, method_shadow -> Conflict(k));
  1928.             }
  1929.         }
  1930.     }
  1931.  
  1932.     //
  1933.     // Compute the set of final variables (all fields in an interface are final) in this type.
  1934.     //
  1935.     Tuple<VariableSymbol *> finals(this_type -> NumVariableSymbols());
  1936.     for (int j = 0; j < this_type -> NumVariableSymbols(); j++)
  1937.     {
  1938.         VariableSymbol *variable_symbol = this_type -> VariableSym(j);
  1939.         finals.Next() = variable_symbol;
  1940.     }
  1941.  
  1942.     //
  1943.     // Initialize each variable, in turn, and check to see if we need to declare a static initialization method: <clinit>.
  1944.     //
  1945.     MethodSymbol *init_method = NULL;
  1946.     for (int k = 0; k < interface_declaration -> NumClassVariables(); k++)
  1947.     {
  1948.         InitializeVariable(interface_declaration -> ClassVariable(k), finals);
  1949.  
  1950.         //
  1951.         // We need a static constructor-initializer if we encounter at least one class
  1952.         // variable that is declared with an initialization expression that is not a
  1953.         // constant expression.
  1954.         //
  1955.         if ((! init_method) && NeedsInitializationMethod(interface_declaration -> ClassVariable(k)))
  1956.         {
  1957.             MethodSymbol *init_method = this_type -> InsertMethodSymbol(control.clinit_name_symbol);
  1958.  
  1959.             init_method -> SetType(control.void_type);
  1960.             init_method -> SetACC_FINAL();
  1961.             init_method -> SetACC_STATIC();
  1962.             init_method -> SetContainingType(this_type);
  1963.             init_method -> SetBlockSymbol(new BlockSymbol(0)); // the symbol table associated with this block will contain no element
  1964.             init_method -> block_symbol -> max_variable_index = 0;
  1965.             init_method -> SetSignature(control);
  1966.  
  1967.             //
  1968.             //
  1969.             //
  1970.             init_method -> max_block_depth = 2; // TODO: Dave why is this the case? We need a legitimate comment here !!!
  1971.             init_method -> block_symbol -> CompressSpace(); // space optimization
  1972.  
  1973.             this_type -> static_initializer_method = init_method;
  1974.         }
  1975.     }
  1976.  
  1977.     //
  1978.     // Recursively process all inner types
  1979.     //
  1980.     for (int l = 0; l < this_type -> NumNestedTypes(); l++)
  1981.     {
  1982.         TypeSymbol *inner_type = this_type -> NestedType(l);
  1983.         if (inner_type -> ACC_INTERFACE())
  1984.             CompleteSymbolTable((AstInterfaceDeclaration *) inner_type -> declaration);
  1985.         else
  1986.         {
  1987.             AstClassDeclaration *class_declaration = (AstClassDeclaration *) inner_type -> declaration;
  1988.             CompleteSymbolTable(class_declaration -> semantic_environment,
  1989.                                 class_declaration -> identifier_token, class_declaration -> class_body);
  1990.         }
  1991.     }
  1992.  
  1993.     state_stack.Pop();
  1994.  
  1995.     return;
  1996. }
  1997.  
  1998.  
  1999. //
  2000. // Pass 5: Free up unneeded space.
  2001. //
  2002. void Semantic::CleanUp()
  2003. {
  2004.     if (control.option.nocleanup)
  2005.     return;
  2006.  
  2007.     for (int i = 0; i < compilation_unit -> NumTypeDeclarations(); i++)
  2008.     {
  2009.         TypeSymbol *type = NULL;
  2010.         Ast *type_declaration = compilation_unit -> TypeDeclaration(i);
  2011.         switch(type_declaration -> kind)
  2012.         {
  2013.             case Ast::CLASS:
  2014.             {
  2015.                 AstClassDeclaration *class_declaration = (AstClassDeclaration *) type_declaration;
  2016.                 if (class_declaration -> semantic_environment)
  2017.                     type = class_declaration -> semantic_environment -> Type();
  2018.                 break;
  2019.             }
  2020.             case Ast::INTERFACE:
  2021.             {
  2022.                 AstInterfaceDeclaration *interface_declaration = (AstInterfaceDeclaration *) type_declaration;
  2023.                 if (interface_declaration -> semantic_environment)
  2024.                     type = interface_declaration -> semantic_environment -> Type();
  2025.                 break;
  2026.             }
  2027.             case Ast::EMPTY_DECLARATION:
  2028.                  break;
  2029.             default:
  2030.                  assert(false);
  2031.                  break;
  2032.         }
  2033.  
  2034.         if (type)
  2035.             CleanUpType(type);
  2036.     }
  2037.  
  2038.     return;
  2039. }
  2040.  
  2041.  
  2042. void Semantic::CleanUpType(TypeSymbol *type)
  2043. {
  2044.     type -> DeleteAnonymousTypes();
  2045.     for (int i = 0; i < type -> NumNestedTypes(); i++)
  2046.         CleanUpType(type -> NestedType(i));
  2047.  
  2048.     type -> CompressSpace(); // space optimization
  2049.  
  2050.     for (int j = 0; j < type -> NumMethodSymbols(); j++)
  2051.         type -> MethodSym(j) -> CleanUp();
  2052.  
  2053.     delete type -> local;
  2054.     if (control.option.nocleanup)
  2055.         return;
  2056.  
  2057.     type -> local = NULL;
  2058.  
  2059.     delete type -> non_local;
  2060.     type -> non_local = NULL;
  2061.  
  2062.     delete type -> semantic_environment;
  2063.     type -> semantic_environment = NULL;
  2064.  
  2065.     type -> declaration = NULL;
  2066.  
  2067.     return;
  2068. }
  2069.  
  2070.  
  2071. TypeSymbol *Semantic::ReadType(FileSymbol *file_symbol, PackageSymbol *package, NameSymbol *name_symbol, LexStream::TokenIndex tok)
  2072. {
  2073.     TypeSymbol *type;
  2074.  
  2075.     if (file_symbol && file_symbol -> IsJava())
  2076.     {
  2077.         if (! file_symbol -> semantic)
  2078.             control.ProcessHeaders(file_symbol);
  2079.         type = package -> FindTypeSymbol(name_symbol);
  2080.         if (! type)
  2081.         {
  2082.             type = package -> InsertOuterTypeSymbol(name_symbol);
  2083.             type -> MarkBad();
  2084.             type -> outermost_type = type;
  2085.             type -> supertypes_closure = new SymbolSet;
  2086.             type -> subtypes = new SymbolSet;
  2087.             type -> semantic_environment = new SemanticEnvironment((Semantic *) this, type, NULL);
  2088.             if (type != control.Object())
  2089.                 type -> super = (type == control.Throwable() ? control.Object() : control.Throwable());
  2090.             type -> SetOwner(package);
  2091.             type -> SetSignature(control);
  2092.             AddDefaultConstructor(type);
  2093.             type -> file_symbol = file_symbol;
  2094.             file_symbol -> types.Next() = type;
  2095.  
  2096.             ReportSemError(SemanticError::TYPE_NOT_FOUND,
  2097.                            tok,
  2098.                            tok,
  2099.                            type -> ContainingPackage() -> PackageName(),
  2100.                            type -> ExternalName());
  2101.         }
  2102.     }
  2103.     else // Read class file.
  2104.     {
  2105.         type = package -> InsertOuterTypeSymbol(name_symbol);
  2106.         type -> outermost_type = type;
  2107.         type -> supertypes_closure = new SymbolSet;
  2108.         type -> subtypes = new SymbolSet;
  2109.         type -> SetOwner(package);
  2110.         type -> SetSignature(control);
  2111.  
  2112.         if (file_symbol)
  2113.         {
  2114.             type -> file_symbol = file_symbol;
  2115.             type -> SetLocation();
  2116.  
  2117.             file_symbol -> package = package;
  2118.             file_symbol -> types.Next() = type;
  2119.  
  2120.             ReadClassFile(type, tok);
  2121.  
  2122.             assert (! type -> IsNested());
  2123.  
  2124.             control.input_class_file_set.AddElement(file_symbol);
  2125.         }
  2126.         else
  2127.         {
  2128.             control.ProcessBadType(type);
  2129.             type -> MarkBad();
  2130.             if (type != control.Object())
  2131.                 type -> super = (type == control.Throwable() ? control.Object() : control.Throwable());
  2132.             AddDefaultConstructor(type);
  2133.  
  2134.             ReportSemError(SemanticError::TYPE_NOT_FOUND,
  2135.                            tok,
  2136.                            tok,
  2137.                            type -> ContainingPackage() -> PackageName(),
  2138.                            type -> ExternalName());
  2139.  
  2140.             if (package == control.unnamed_package)
  2141.             {
  2142.                 TypeSymbol *old_type = (TypeSymbol *) control.unnamed_package_types.Image(type -> Identity());
  2143.                 if (! old_type)
  2144.                     control.unnamed_package_types.AddElement(type);
  2145.                 else
  2146.                 {
  2147.                     ReportSemError(SemanticError::DUPLICATE_TYPE_DECLARATION,
  2148.                                    tok,
  2149.                                    tok,
  2150.                                    type -> Name(),
  2151.                                    old_type -> FileLoc());
  2152.                 }
  2153.             }
  2154.         }
  2155.     }
  2156.  
  2157.     return type;
  2158. }
  2159.  
  2160.  
  2161. TypeSymbol *Semantic::GetBadNestedType(TypeSymbol *type, LexStream::TokenIndex identifier_token)
  2162. {
  2163.     NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  2164.  
  2165.     TypeSymbol *outermost_type = type -> outermost_type;
  2166.     if (! outermost_type -> non_local)
  2167.         outermost_type -> non_local = new SymbolSet;
  2168.     if (! outermost_type -> local)
  2169.         outermost_type -> local = new SymbolSet;
  2170.  
  2171.     int length = type -> ExternalNameLength() + 1 + name_symbol -> NameLength(); // +1 for $,... +1 for $
  2172.     wchar_t *external_name = new wchar_t[length + 1]; // +1 for '\0';
  2173.     wcscpy(external_name, type -> ExternalName());
  2174.     wcscat(external_name, StringConstant::US__DS);
  2175.     wcscat(external_name, name_symbol -> Name());
  2176.  
  2177.     TypeSymbol *inner_type = type -> InsertNestedTypeSymbol(name_symbol);
  2178.     inner_type -> MarkBad();
  2179.     inner_type -> outermost_type = type -> outermost_type;
  2180.     inner_type -> supertypes_closure = new SymbolSet;
  2181.     inner_type -> subtypes = new SymbolSet;
  2182.     inner_type -> SetExternalIdentity(control.FindOrInsertName(external_name, length));
  2183.     inner_type -> semantic_environment = new SemanticEnvironment((Semantic *) this,
  2184.                                                                  inner_type,
  2185.                                                                  type -> semantic_environment);
  2186.     inner_type -> super = control.Object();
  2187.     inner_type -> SetOwner(type);
  2188.     inner_type -> SetSignature(control);
  2189.     inner_type -> InsertThis(0);
  2190.     AddDefaultConstructor(inner_type);
  2191.  
  2192.     ReportSemError(SemanticError::TYPE_NOT_FOUND,
  2193.                    identifier_token,
  2194.                    identifier_token,
  2195.                    inner_type -> ContainingPackage() -> PackageName(),
  2196.                    inner_type -> ExternalName());
  2197.  
  2198.     delete [] external_name;
  2199.  
  2200.     return inner_type;
  2201. }
  2202.  
  2203.  
  2204. void Semantic::ProcessImportQualifiedName(AstExpression *name)
  2205. {
  2206.     AstFieldAccess *field_access = name -> FieldAccessCast();
  2207.     if (field_access)
  2208.     {
  2209.         ProcessImportQualifiedName(field_access -> base);
  2210.         Symbol *symbol = field_access -> base -> symbol;
  2211.  
  2212.         TypeSymbol *type = symbol -> TypeCast();
  2213.         if (type) // The base name is a type
  2214.         {
  2215.             if (! type -> NestedTypesProcessed())
  2216.                 type -> ProcessNestedTypeSignatures((Semantic *) this, field_access -> identifier_token);
  2217.             NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  2218.             TypeSymbol *inner_type = type -> FindTypeSymbol(name_symbol);
  2219.             if (! inner_type)
  2220.                  inner_type = GetBadNestedType(type, field_access -> identifier_token);
  2221.             else if (! (inner_type -> ACC_PUBLIC() || inner_type -> ContainingPackage() == this_package))
  2222.                  ReportTypeInaccessible(field_access, inner_type);
  2223.             type = inner_type;
  2224.             field_access -> symbol = type; // save the type to which this expression was resolved for later use...
  2225.         }
  2226.         else
  2227.         {
  2228.             PackageSymbol *package = symbol -> PackageCast();
  2229.             NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  2230.             type = package -> FindTypeSymbol(name_symbol);
  2231.             if (! type)
  2232.             {
  2233.                 FileSymbol *file_symbol = Control::GetFile(control, package, name_symbol);
  2234.                 if (file_symbol)
  2235.                     type = ReadType(file_symbol, package, name_symbol, field_access -> identifier_token);
  2236.             }
  2237.             else if (type -> SourcePending())
  2238.                  control.ProcessHeaders(type -> file_symbol);
  2239.  
  2240.             //
  2241.             // If the field_access was resolved to a type, save it later use.
  2242.             // Otherwise, assume the field_access is a package name.
  2243.             //
  2244.             if (type)
  2245.                  field_access -> symbol = type;
  2246.             else
  2247.             {
  2248.                 NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  2249.                 PackageSymbol *subpackage = package -> FindPackageSymbol(name_symbol);
  2250.                 if (! subpackage)
  2251.                     subpackage = package -> InsertPackageSymbol(name_symbol);
  2252.                 control.FindPathsToDirectory(subpackage);
  2253.                 field_access -> symbol = subpackage;
  2254.             }
  2255.         }
  2256.     }
  2257.     else
  2258.     {
  2259.         AstSimpleName *simple_name = name -> SimpleNameCast();
  2260.  
  2261.         assert(simple_name);
  2262.  
  2263.         //
  2264.         // From the 1.1 document:
  2265.         //
  2266.         //    Nested classes of all sorts (top-level or inner) can be imported by either kind of
  2267.         //    import statement. Class names in import statements must be fully package
  2268.         //    qualified, and be resolvable without reference to inheritance relations...
  2269.         //
  2270.         TypeSymbol *type;
  2271.         if (compilation_unit -> package_declaration_opt)
  2272.         {
  2273.             type = FindSimpleNameType(this_package, simple_name -> identifier_token);
  2274.             //
  2275.             // If the type was not found, look for it in the unnamed package.
  2276.             // The relevant passages that justify this lookup are:
  2277.             // 6.5.4.11, 6.7, 7.4.2, 7.5.1
  2278.             //
  2279.             if (! type)
  2280.                 type = FindSimpleNameType(control.unnamed_package, simple_name -> identifier_token);
  2281.         }
  2282.         else type = FindSimpleNameType(control.unnamed_package, simple_name -> identifier_token);
  2283.  
  2284.         //
  2285.         // If the simple_name is a type, save it. Otherwise, assume it is a package
  2286.         //
  2287.         if (type)
  2288.         {
  2289.             if (! (type -> ACC_PUBLIC() || type -> ContainingPackage() == this_package))
  2290.                 ReportTypeInaccessible(name, type);
  2291.             simple_name -> symbol = type;
  2292.         }
  2293.         else
  2294.         {
  2295.             NameSymbol *name_symbol = lex_stream -> NameSymbol(simple_name -> identifier_token);
  2296.             PackageSymbol *package = control.external_table.FindPackageSymbol(name_symbol);
  2297.             if (! package)
  2298.                 package = control.external_table.InsertPackageSymbol(name_symbol, NULL);
  2299.             control.FindPathsToDirectory(package);
  2300.             simple_name -> symbol = package;
  2301.         }
  2302.     }
  2303.  
  2304.     return;
  2305. }
  2306.  
  2307.  
  2308. void Semantic::ProcessPackageOrType(AstExpression *name)
  2309. {
  2310.     AstFieldAccess *field_access = name -> FieldAccessCast();
  2311.     if (field_access)
  2312.     {
  2313.         ProcessPackageOrType(field_access -> base);
  2314.         Symbol *symbol = field_access -> base -> symbol;
  2315.  
  2316.         TypeSymbol *type = symbol -> TypeCast();
  2317.         if (type) // The base name is a type
  2318.         {
  2319.             type = MustFindNestedType(type, field_access);
  2320.             field_access -> symbol = type; // save the type to which this expression was resolved for later use...
  2321.         }
  2322.         else
  2323.         {
  2324.             PackageSymbol *package = symbol -> PackageCast();
  2325.             NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  2326.             type = package -> FindTypeSymbol(name_symbol);
  2327.             if (! type)
  2328.             {
  2329.                 FileSymbol *file_symbol = Control::GetFile(control, package, name_symbol);
  2330.                 if (file_symbol)
  2331.                     type = ReadType(file_symbol, package, name_symbol, field_access -> identifier_token);
  2332.             }
  2333.             else if (type -> SourcePending())
  2334.                  control.ProcessHeaders(type -> file_symbol);
  2335.  
  2336.             //
  2337.             // If the field access was resolved into a type, then save it.
  2338.             // Otherwise, assume it is a package
  2339.             //
  2340.             if (type)
  2341.                  field_access -> symbol = type; // save the type to which this expression was resolved for later use...
  2342.             else
  2343.             {
  2344.                 NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  2345.                 PackageSymbol *subpackage = package -> FindPackageSymbol(name_symbol);
  2346.                 if (! subpackage)
  2347.                     subpackage = package -> InsertPackageSymbol(name_symbol);
  2348.                 control.FindPathsToDirectory(subpackage);
  2349.                 field_access -> symbol = subpackage;
  2350.             }
  2351.         }
  2352.     }
  2353.     else
  2354.     {
  2355.         AstSimpleName *simple_name = name -> SimpleNameCast();
  2356.  
  2357.         assert(simple_name);
  2358.  
  2359.         TypeSymbol *type = FindType(simple_name -> identifier_token);
  2360.         if (type)
  2361.         {
  2362.             TypeAccessCheck(simple_name, type);
  2363.             simple_name -> symbol = type;
  2364.         }
  2365.         else
  2366.         {
  2367.             NameSymbol *name_symbol = lex_stream -> NameSymbol(simple_name -> identifier_token);
  2368.             PackageSymbol *package = control.external_table.FindPackageSymbol(name_symbol);
  2369.             if (! package)
  2370.                 package = control.external_table.InsertPackageSymbol(name_symbol, NULL);
  2371.             control.FindPathsToDirectory(package);
  2372.             simple_name -> symbol = package;
  2373.         }
  2374.     }
  2375.  
  2376.     return;
  2377. }
  2378.  
  2379.  
  2380. void Semantic::ProcessTypeImportOnDemandDeclaration(AstImportDeclaration *import_declaration)
  2381. {
  2382.     ProcessImportQualifiedName(import_declaration -> name);
  2383.     Symbol *symbol = import_declaration -> name -> symbol;
  2384.  
  2385.     PackageSymbol *package = symbol -> PackageCast();
  2386.     if (package && package -> directory.Length() == 0)
  2387.     {
  2388.         ReportSemError(SemanticError::PACKAGE_NOT_FOUND,
  2389.                        import_declaration -> name -> LeftToken(),
  2390.                        import_declaration -> name -> RightToken(),
  2391.                        package -> PackageName());
  2392.     }
  2393.  
  2394.     //
  2395.     // Two or more type-import-on-demand may name the same package; the effect is as if there
  2396.     // were only one such declaration.
  2397.     //
  2398.     for (int i = 0; i < import_on_demand_packages.Length(); i++)
  2399.     {
  2400.         if (symbol == import_on_demand_packages[i])
  2401.             return;
  2402.     }
  2403.  
  2404.     import_on_demand_packages.Next() = symbol;
  2405.  
  2406.     //
  2407.     //
  2408.     //
  2409.     TypeSymbol *type = symbol -> TypeCast();
  2410.     if (control.option.deprecation && type && type -> IsDeprecated() && type -> file_symbol != source_file_symbol)
  2411.     {
  2412.         ReportSemError(SemanticError::DEPRECATED_TYPE,
  2413.                        import_declaration -> name -> LeftToken(),
  2414.                        import_declaration -> name -> RightToken(),
  2415.                        type -> ContainingPackage() -> PackageName(),
  2416.                        type -> ExternalName());
  2417.     }
  2418.  
  2419.     return;
  2420. }
  2421.  
  2422.  
  2423. //
  2424. // The Ast name is a name expression (either a qualified name or a simplename)
  2425. // FindFirstType traverses the name tree and returns the first subtree that it
  2426. // finds that matches a type. As a side-effect, each subtree that matches a package
  2427. // or a type has that package or type recorded in its "symbol" field.
  2428. //
  2429. AstExpression *Semantic::FindFirstType(Ast *name)
  2430. {
  2431.     AstExpression *name_expression = NULL;
  2432.  
  2433.     AstFieldAccess *field_access = name -> FieldAccessCast();
  2434.     if (field_access)
  2435.     {
  2436.         AstExpression *expr = FindFirstType(field_access -> base);
  2437.  
  2438.         if (expr -> symbol -> TypeCast()) // A subexpression has been found, pass it up
  2439.             name_expression = expr;
  2440.         else
  2441.         {
  2442.             PackageSymbol *package = expr -> symbol -> PackageCast();
  2443.  
  2444.             assert(package);
  2445.  
  2446.             name_expression = field_access; // The relevant subexpression might be this field access...
  2447.  
  2448.             NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  2449.             TypeSymbol *type = package -> FindTypeSymbol(name_symbol);
  2450.             if (type)
  2451.             {
  2452.                 if (type -> SourcePending())
  2453.                     control.ProcessHeaders(type -> file_symbol);
  2454.                 field_access -> symbol = type;
  2455.             }
  2456.             else
  2457.             {
  2458.                 FileSymbol *file_symbol = Control::GetFile(control, package, name_symbol);
  2459.                 if (file_symbol)
  2460.                     field_access -> symbol = ReadType(file_symbol, package, name_symbol, field_access -> identifier_token);
  2461.                 else
  2462.                 {
  2463.                     PackageSymbol *subpackage = package -> FindPackageSymbol(name_symbol);
  2464.                     if (! subpackage)
  2465.                         subpackage = package -> InsertPackageSymbol(name_symbol);
  2466.                     control.FindPathsToDirectory(subpackage);
  2467.                     field_access -> symbol = subpackage;
  2468.                 }
  2469.             }
  2470.         }
  2471.     }
  2472.     else
  2473.     {
  2474.         AstSimpleName *simple_name = name -> SimpleNameCast();
  2475.  
  2476.         assert(simple_name);
  2477.  
  2478.         ProcessPackageOrType(simple_name);
  2479.         name_expression = simple_name;
  2480.     }
  2481.  
  2482.     return name_expression;
  2483. }
  2484.  
  2485.  
  2486. TypeSymbol *Semantic::FindSimpleNameType(PackageSymbol *package, LexStream::TokenIndex identifier_token)
  2487. {
  2488.     NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  2489.     TypeSymbol *type = package -> FindTypeSymbol(name_symbol);
  2490.     if (type)
  2491.     {
  2492.         if (type -> SourcePending())
  2493.              control.ProcessHeaders(type -> file_symbol);
  2494.     }
  2495.     else
  2496.     {
  2497.         //
  2498.         // Check whether or not the type was declared in another compilation unit
  2499.         // in the main package.
  2500.         //
  2501.         FileSymbol *file_symbol = Control::GetFile(control, package, name_symbol);
  2502.         if (file_symbol)
  2503.             type = ReadType(file_symbol, package, name_symbol, identifier_token);
  2504.     }
  2505.  
  2506.     return type;
  2507. }
  2508.  
  2509. void Semantic::ProcessSingleTypeImportDeclaration(AstImportDeclaration *import_declaration)
  2510. {
  2511.     ProcessImportQualifiedName(import_declaration -> name);
  2512.     Symbol *symbol = import_declaration -> name -> symbol;
  2513.     PackageSymbol *package = symbol -> PackageCast();
  2514.     if (package)
  2515.     {
  2516.         ReportSemError(SemanticError::UNKNOWN_QUALIFIED_NAME_BASE,
  2517.                        import_declaration -> name -> LeftToken(),
  2518.                        import_declaration -> name -> RightToken(),
  2519.                        package -> PackageName());
  2520.         return;
  2521.     }
  2522.  
  2523.     TypeSymbol *type = symbol -> TypeCast();
  2524.  
  2525.     //
  2526.     // If two single-type-import declarations in the same compilation unit attempt to
  2527.     // import types with the same simple name, then a compile-time error occurs, unless
  2528.     // the two types are the same type, in which case the duplicate declaration is ignored.
  2529.     //
  2530.     for (int i = 0; i < single_type_imports.Length(); i++)
  2531.     {
  2532.         if (type == single_type_imports[i])
  2533.             return;
  2534.     }
  2535.  
  2536.     TypeSymbol *old_type;
  2537.     int k;
  2538.     for (k = 0; k < compilation_unit -> NumTypeDeclarations(); k++)
  2539.     {
  2540.         AstClassDeclaration *class_declaration = compilation_unit -> TypeDeclaration(k) -> ClassDeclarationCast();
  2541.         AstInterfaceDeclaration *interface_declaration = compilation_unit -> TypeDeclaration(k) -> InterfaceDeclarationCast();
  2542.  
  2543.         if (class_declaration)
  2544.         {
  2545.             if (class_declaration -> semantic_environment)
  2546.             {
  2547.                 old_type = class_declaration -> semantic_environment -> Type();
  2548.                 if (old_type -> Identity() == type -> Identity())
  2549.                     break;
  2550.             }
  2551.         }
  2552.         else if (interface_declaration)
  2553.         {
  2554.             if (interface_declaration -> semantic_environment)
  2555.             {
  2556.                 old_type = interface_declaration -> semantic_environment -> Type();
  2557.                 if (old_type -> Identity() == type -> Identity())
  2558.                     break;
  2559.             }
  2560.         }
  2561.     }
  2562.  
  2563.     if (k < compilation_unit -> NumTypeDeclarations())
  2564.     {
  2565.         AstFieldAccess *field_access = import_declaration -> name -> FieldAccessCast();
  2566.         package = (field_access ? field_access -> base -> symbol -> PackageCast() : control.unnamed_package);
  2567.  
  2568.         //
  2569.         // It's ok to import a type that is being compiled...
  2570.         //
  2571.         if (type == old_type && package == this_package)
  2572.         {
  2573.             ReportSemError(SemanticError::UNNECESSARY_TYPE_IMPORT,
  2574.                            import_declaration -> name -> LeftToken(),
  2575.                            import_declaration -> name -> RightToken(),
  2576.                            lex_stream -> NameString(import_declaration -> name -> RightToken()),
  2577.                            old_type -> FileLoc());
  2578.         }
  2579.         else
  2580.         {
  2581.             ReportSemError(SemanticError::DUPLICATE_TYPE_DECLARATION,
  2582.                            import_declaration -> name -> LeftToken(),
  2583.                            import_declaration -> name -> RightToken(),
  2584.                            lex_stream -> NameString(import_declaration -> name -> RightToken()),
  2585.                            old_type -> FileLoc());
  2586.         }
  2587.     }
  2588.     else
  2589.     {
  2590.         int i = 0;
  2591.         for (i = 0; i < compilation_unit -> NumImportDeclarations(); i++)
  2592.         {
  2593.             TypeSymbol *other_type = compilation_unit -> ImportDeclaration(i) -> name -> Type();
  2594.             if ((compilation_unit -> ImportDeclaration(i) == import_declaration) ||
  2595.                 (other_type && other_type -> Identity() == type -> Identity()))
  2596.                 break;
  2597.         }
  2598.  
  2599.         assert(i < compilation_unit -> NumImportDeclarations());
  2600.  
  2601.         if (compilation_unit -> ImportDeclaration(i) == import_declaration) // No duplicate found
  2602.         {
  2603.             import_declaration -> name -> symbol = type;
  2604.             single_type_imports.Next() = type;
  2605.         }
  2606.         else
  2607.         {
  2608.             FileLocation file_location(lex_stream, compilation_unit -> ImportDeclaration(i) -> LeftToken());
  2609.             ReportSemError(SemanticError::DUPLICATE_TYPE_DECLARATION,
  2610.                            import_declaration -> name -> LeftToken(),
  2611.                            import_declaration -> name -> RightToken(),
  2612.                            lex_stream -> NameString(import_declaration -> name -> RightToken()),
  2613.                            file_location.location);
  2614.         }
  2615.     }
  2616.  
  2617.     if (! (type -> ACC_PUBLIC() || type -> ContainingPackage() == this_package))
  2618.         ReportTypeInaccessible(import_declaration -> name, type);
  2619.  
  2620.     //
  2621.     //
  2622.     //
  2623.     if (control.option.deprecation && type -> IsDeprecated() && type -> file_symbol != source_file_symbol)
  2624.     {
  2625.         ReportSemError(SemanticError::DEPRECATED_TYPE,
  2626.                        import_declaration -> name -> LeftToken(),
  2627.                        import_declaration -> name -> RightToken(),
  2628.                        type -> ContainingPackage() -> PackageName(),
  2629.                        type -> ExternalName());
  2630.     }
  2631.  
  2632.     return;
  2633. }
  2634.  
  2635.  
  2636. void Semantic::ProcessFieldDeclaration(AstFieldDeclaration *field_declaration)
  2637. {
  2638.     TypeSymbol *this_type = ThisType();
  2639.     AccessFlags access_flags = (this_type -> ACC_INTERFACE()
  2640.                                            ? ProcessConstantModifiers(field_declaration)
  2641.                                            : ProcessFieldModifiers(field_declaration));
  2642.  
  2643.     //
  2644.     // New feature in java 1.2 that is undocumented in the 1.1 document.
  2645.     // A field may be declared static iff it is final and not blank-final...
  2646.     //
  2647.     if (access_flags.ACC_STATIC() && (! access_flags.ACC_FINAL()) && this_type -> IsInner())
  2648.     {
  2649.         AstModifier *modifier = NULL;
  2650.         for (int i = 0; i < field_declaration -> NumVariableModifiers(); i++)
  2651.         {
  2652.             if (field_declaration -> VariableModifier(i) -> kind == Ast::STATIC)
  2653.                 modifier = field_declaration -> VariableModifier(i);
  2654.         }
  2655.  
  2656.         assert(modifier);
  2657.  
  2658.         ReportSemError(SemanticError::STATIC_FIELD_IN_INNER_CLASS,
  2659.                        modifier -> modifier_kind_token,
  2660.                        modifier -> modifier_kind_token);
  2661.     }
  2662.  
  2663.     //
  2664.     //
  2665.     //
  2666.     bool deprecated_declarations = lex_stream -> IsDeprecated(lex_stream -> Previous(field_declaration -> LeftToken()));
  2667.     AstArrayType *array_type = field_declaration -> type -> ArrayTypeCast();
  2668.     Ast *actual_type = (array_type ? array_type -> type : field_declaration -> type);
  2669.     AstPrimitiveType *primitive_type = actual_type -> PrimitiveTypeCast();
  2670.     TypeSymbol *field_type = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(actual_type));
  2671.     for (int i = 0; i < field_declaration -> NumVariableDeclarators(); i++)
  2672.     {
  2673.         AstVariableDeclarator *variable_declarator = field_declaration -> VariableDeclarator(i);
  2674.         AstVariableDeclaratorId *name = variable_declarator -> variable_declarator_name;
  2675.         NameSymbol *name_symbol = lex_stream -> NameSymbol(name -> identifier_token);
  2676.  
  2677.         if (this_type -> FindVariableSymbol(name_symbol))
  2678.         {
  2679.             ReportSemError(SemanticError::DUPLICATE_FIELD,
  2680.                            name -> identifier_token,
  2681.                            name -> identifier_token,
  2682.                            name_symbol -> Name(),
  2683.                            this_type -> Name());
  2684.         }
  2685.         else
  2686.         {
  2687.             VariableSymbol *variable = this_type -> InsertVariableSymbol(name_symbol);
  2688.             int num_dimensions = (array_type ? array_type -> NumBrackets() : 0) + name -> NumBrackets();
  2689.             if (num_dimensions == 0)
  2690.                  variable -> SetType(field_type);
  2691.             else variable -> SetType(field_type -> GetArrayType((Semantic *) this, num_dimensions));
  2692.             variable -> SetFlags(access_flags);
  2693.             variable -> SetOwner(this_type);
  2694.             variable -> declarator = variable_declarator;
  2695.             variable -> MarkIncomplete(); // the declaration of a field is not complete until its initializer
  2696.                                           // (if any) has been processed.
  2697.             variable_declarator -> symbol = variable;
  2698.  
  2699.             if (deprecated_declarations)
  2700.                 variable -> MarkDeprecated();
  2701.         }
  2702.     }
  2703.  
  2704.     return;
  2705. }
  2706.  
  2707.  
  2708. void Semantic::GenerateLocalConstructor(MethodSymbol *constructor)
  2709. {
  2710.     TypeSymbol *local_type = constructor -> containing_type;
  2711.  
  2712.     //
  2713.     // Make up external name for constructor as we shall turn it into a regular method later.
  2714.     // Note that the method needs to be PRIVATE and FINAL so that:
  2715.     //
  2716.     //    1. virtual calls are not made to it
  2717.     //
  2718.     //    2. it might be inlined later.
  2719.     //
  2720.     // This resetting destroys the original access flags specified by the user. We shall
  2721.     // say more about this later...
  2722.     //
  2723.     IntToWstring value(local_type -> NumGeneratedConstructors());
  2724.  
  2725.     int length = 12 + value.Length(); // +12 for constructor$
  2726.     wchar_t *external_name = new wchar_t[length + 1]; // +1 for '\0';
  2727.     wcscpy(external_name, StringConstant::US__constructor_DOLLAR);
  2728.     wcscat(external_name, value.String());
  2729.     constructor -> SetExternalIdentity(control.FindOrInsertName(external_name, length)); // Turn the constructor into a method
  2730.     constructor -> ResetFlags();
  2731.     constructor -> SetACC_PRIVATE();
  2732.     constructor -> SetACC_FINAL();
  2733.  
  2734.     delete [] external_name;
  2735.  
  2736.     //
  2737.     // Make generated constructor symbol. The associated symbol table will not contain too many elements.
  2738.     //
  2739.     BlockSymbol *block_symbol = new BlockSymbol(local_type -> NumConstructorParameters() +
  2740.                                                 constructor -> NumFormalParameters() + 3);
  2741.     block_symbol -> max_variable_index = 1; // All types need a spot for "this"
  2742.  
  2743.     //
  2744.     // Note that the local constructor does not inherit the access flags of the specified constructor.
  2745.     // This is because it acts more like a read_access method. The synthetic attribute that is associated
  2746.     // with the constructor allows the compiler to prevent an illegal access from an unauthorized environment.
  2747.     //
  2748.     MethodSymbol *local_constructor = local_type -> LocalConstructorOverload(constructor);
  2749.     local_constructor -> MarkSynthetic();
  2750.     local_constructor -> method_or_constructor_declaration = constructor -> method_or_constructor_declaration;
  2751.     local_constructor -> SetType(control.void_type);
  2752.     local_constructor -> SetContainingType(local_type);
  2753.     local_constructor -> SetBlockSymbol(block_symbol);
  2754.     for (int i = 0; i < constructor -> NumThrows(); i++)
  2755.         local_constructor -> AddThrows(constructor -> Throws(i));
  2756.  
  2757.     for (int j = 0; j < local_type -> NumConstructorParameters(); j++)
  2758.     {
  2759.         VariableSymbol *param = local_type -> ConstructorParameter(j),
  2760.                        *symbol = block_symbol -> InsertVariableSymbol(param -> Identity());
  2761.         symbol -> MarkSynthetic();
  2762.         symbol -> SetType(param -> Type());
  2763.         symbol -> SetOwner(local_constructor);
  2764.         symbol -> SetExternalIdentity(param -> ExternalIdentity());
  2765.         symbol -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  2766.         if (control.IsDoubleWordType(symbol -> Type()))
  2767.             block_symbol -> max_variable_index++;
  2768.         local_constructor -> AddFormalParameter(symbol);
  2769.     }
  2770.  
  2771.     //
  2772.     // Add all the parameters from the original constructor to the symbol
  2773.     // table of the local constructor. However, only mark them complete and
  2774.     // do not yet assign a number to them. This will be done after we know
  2775.     // how many extra "local" variable shadows are needed.
  2776.     //
  2777.     for (int k = 0; k < constructor -> NumFormalParameters(); k++)
  2778.     {
  2779.         VariableSymbol *param = constructor -> FormalParameter(k),
  2780.                        *symbol = block_symbol -> InsertVariableSymbol(param -> Identity());
  2781.         symbol -> MarkSynthetic();
  2782.         symbol -> MarkComplete();
  2783.         symbol -> SetType(param -> Type());
  2784.         symbol -> SetOwner(local_constructor);
  2785.     }
  2786.  
  2787.     local_type -> AddGeneratedConstructor(local_constructor);
  2788.  
  2789.     return;
  2790. }
  2791.  
  2792.  
  2793. void Semantic::ProcessConstructorDeclaration(AstConstructorDeclaration *constructor_declaration)
  2794. {
  2795.     TypeSymbol *this_type = ThisType();
  2796.     if (this_type -> Anonymous())
  2797.     {
  2798.         ReportSemError(SemanticError::CONSTRUCTOR_FOUND_IN_ANONYMOUS_CLASS,
  2799.                        constructor_declaration -> LeftToken(),
  2800.                        constructor_declaration -> RightToken());
  2801.         return;
  2802.     }
  2803.  
  2804.     AccessFlags access_flags = ProcessConstructorModifiers(constructor_declaration);
  2805.  
  2806.     AstMethodDeclarator *constructor_declarator = constructor_declaration -> constructor_declarator;
  2807.     wchar_t *constructor_name = lex_stream -> NameString(constructor_declarator -> identifier_token);
  2808.  
  2809.     if (lex_stream -> NameSymbol(constructor_declarator -> identifier_token) != this_type -> Identity())
  2810.     {
  2811.         ReportSemError(SemanticError::MISMATCHED_CONSTRUCTOR_NAME,
  2812.                        constructor_declarator -> identifier_token,
  2813.                        constructor_declarator -> identifier_token,
  2814.                        constructor_name,
  2815.                        this_type -> Name());
  2816.         constructor_name = this_type -> Name(); // assume the proper name !
  2817.     }
  2818.  
  2819.     //
  2820.     // As the body of the constructor may not have been parsed yet, we estimate a size
  2821.     // for its symbol table based on the number of lines in the body + a margin for one-liners.
  2822.     //
  2823.     BlockSymbol *block_symbol = new BlockSymbol(constructor_declarator -> NumFormalParameters() + 3);
  2824.     block_symbol -> max_variable_index = 1; // All types need a spot for "this".
  2825.  
  2826.     ProcessFormalParameters(block_symbol, constructor_declarator);
  2827.  
  2828.     //
  2829.     // Note that constructors are always named "<init>"
  2830.     //
  2831.     MethodSymbol *constructor = this_type -> FindMethodSymbol(control.init_name_symbol);
  2832.  
  2833.     if (! constructor) // there exists a constructor already in type -> table.
  2834.          constructor = this_type -> InsertConstructorSymbol(control.init_name_symbol);
  2835.     else
  2836.     {
  2837.         if (this_type -> FindOverloadMethod(constructor, constructor_declarator))
  2838.         {
  2839.             ReportSemError(SemanticError::DUPLICATE_CONSTRUCTOR,
  2840.                            constructor_declarator -> LeftToken(),
  2841.                            constructor_declarator -> RightToken(),
  2842.                            this_type -> Name());
  2843.             delete block_symbol;
  2844.             return;
  2845.         }
  2846.  
  2847.         constructor = this_type -> Overload(constructor);
  2848.     }
  2849.  
  2850.     //
  2851.     // If the method is not static, leave a slot for the "this" pointer.
  2852.     //
  2853.     constructor -> SetType(control.void_type);
  2854.     constructor -> SetFlags(access_flags);
  2855.     constructor -> SetContainingType(this_type);
  2856.     constructor -> SetBlockSymbol(block_symbol);
  2857.     constructor -> method_or_constructor_declaration = constructor_declaration;
  2858.  
  2859.     VariableSymbol *this0_variable = NULL;
  2860.     if (this_type -> IsInner())
  2861.     {
  2862.         this0_variable = block_symbol -> InsertVariableSymbol(control.this0_name_symbol);
  2863.         this0_variable -> SetType(this_type -> ContainingType());
  2864.         this0_variable -> SetOwner(constructor);
  2865.         this0_variable -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  2866.     }
  2867.  
  2868.     for (int i = 0; i < constructor_declarator -> NumFormalParameters(); i++)
  2869.     {
  2870.         AstVariableDeclarator *formal_declarator = constructor_declarator -> FormalParameter(i) -> formal_declarator;
  2871.         VariableSymbol *symbol = formal_declarator -> symbol;
  2872.  
  2873.         symbol -> SetOwner(constructor);
  2874.         symbol -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  2875.         if (control.IsDoubleWordType(symbol -> Type()))
  2876.             block_symbol -> max_variable_index++;
  2877.         symbol -> declarator = formal_declarator;
  2878.         constructor -> AddFormalParameter(symbol);
  2879.     }
  2880.  
  2881.     constructor -> SetSignature(control, this0_variable);
  2882.  
  2883.     for (int k = 0; k < constructor_declaration -> NumThrows(); k++)
  2884.     {
  2885.         AstExpression *throw_expression = constructor_declaration -> Throw(k);
  2886.         TypeSymbol *throw_type = MustFindType(throw_expression);
  2887.         throw_expression -> symbol = throw_type;
  2888.         constructor -> AddThrows(throw_type);
  2889.     }
  2890.  
  2891.     constructor_declaration -> constructor_symbol = constructor; // save for processing bodies later.
  2892.  
  2893.     if (this_type -> IsLocal())
  2894.         GenerateLocalConstructor(constructor);
  2895.  
  2896.     if (lex_stream -> IsDeprecated(lex_stream -> Previous(constructor_declaration -> LeftToken())))
  2897.         constructor -> MarkDeprecated();
  2898.  
  2899.     return;
  2900. }
  2901.  
  2902.  
  2903. void Semantic::AddDefaultConstructor(TypeSymbol *type)
  2904. {
  2905.     MethodSymbol *constructor = type -> InsertConstructorSymbol(control.init_name_symbol);
  2906.  
  2907.     BlockSymbol *block_symbol = new BlockSymbol(1); // TODO: make sure this size is right !!!
  2908.     block_symbol -> max_variable_index = 1; // All types need a spot for "this"
  2909.  
  2910.     constructor -> SetType(control.void_type);
  2911.     constructor -> SetContainingType(type);
  2912.     constructor -> SetBlockSymbol(block_symbol);
  2913.     if (type -> ACC_PUBLIC())
  2914.         constructor -> SetACC_PUBLIC();
  2915.  
  2916.     VariableSymbol *this0_variable = NULL;
  2917.     if (type -> IsInner())
  2918.     {
  2919.         this0_variable = block_symbol -> InsertVariableSymbol(control.this0_name_symbol);
  2920.         this0_variable -> SetType(type -> ContainingType());
  2921.         this0_variable -> SetOwner(constructor);
  2922.         this0_variable -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  2923.     }
  2924.  
  2925.     constructor -> SetSignature(control, this0_variable);
  2926.  
  2927.     AstClassDeclaration *class_declaration = (type -> declaration ? type -> declaration -> ClassDeclarationCast()
  2928.                                                                   : (AstClassDeclaration *) NULL);
  2929.     if (class_declaration)
  2930.     {
  2931.         AstClassBody *class_body = class_declaration -> class_body;
  2932.         LexStream::TokenIndex left_loc  = class_declaration -> identifier_token,
  2933.                               right_loc = (class_declaration -> super_opt ? class_declaration -> super_opt -> RightToken()
  2934.                                                                           : class_declaration -> identifier_token);
  2935.  
  2936.         AstMethodDeclarator *method_declarator       = compilation_unit -> ast_pool -> GenMethodDeclarator();
  2937.         method_declarator -> identifier_token        = left_loc;
  2938.         method_declarator -> left_parenthesis_token  = left_loc;
  2939.         method_declarator -> right_parenthesis_token = right_loc;
  2940.  
  2941.         AstSuperCall *super_call = NULL;
  2942.         if (type != control.Object())
  2943.         {
  2944.             super_call                            = compilation_unit -> ast_pool -> GenSuperCall();
  2945.             super_call -> base_opt                = NULL;
  2946.             super_call -> dot_token_opt           = 0;
  2947.             super_call -> super_token             = left_loc;
  2948.             super_call -> left_parenthesis_token  = left_loc;
  2949.             super_call -> right_parenthesis_token = right_loc;
  2950.             super_call -> semicolon_token         = right_loc;
  2951.         }
  2952.  
  2953.         AstReturnStatement *return_statement = compilation_unit -> ast_pool -> GenReturnStatement();
  2954.         return_statement -> return_token = left_loc;
  2955.         return_statement -> expression_opt = NULL;
  2956.         return_statement -> semicolon_token = left_loc;
  2957.         return_statement -> is_reachable = true;
  2958.  
  2959.         AstBlock *block = compilation_unit -> ast_pool -> GenBlock();
  2960.         block -> AllocateBlockStatements(1); // this block contains one statement
  2961.         block -> left_brace_token  = left_loc;
  2962.         block -> right_brace_token = right_loc;
  2963.  
  2964.         block -> is_reachable = true;
  2965.         block -> can_complete_normally = false;
  2966.         block -> AddStatement(return_statement);
  2967.  
  2968.         AstConstructorBlock *constructor_block                   = compilation_unit -> ast_pool -> GenConstructorBlock();
  2969.         constructor_block -> left_brace_token                    = left_loc;
  2970.         constructor_block -> explicit_constructor_invocation_opt = super_call;
  2971.         constructor_block -> block                               = block;
  2972.         constructor_block -> right_brace_token                   = right_loc;
  2973.  
  2974.         AstConstructorDeclaration *constructor_declaration = compilation_unit -> ast_pool -> GenConstructorDeclaration();
  2975.         constructor_declaration -> constructor_declarator   = method_declarator;
  2976.         constructor_declaration -> constructor_body         = constructor_block;
  2977.  
  2978.         constructor_declaration -> constructor_symbol = constructor;
  2979.         constructor -> method_or_constructor_declaration = constructor_declaration;
  2980.         class_body -> default_constructor = constructor_declaration;
  2981.  
  2982.         if (type -> IsLocal())
  2983.             GenerateLocalConstructor(constructor);
  2984.     }
  2985.  
  2986.     return;
  2987. }
  2988.  
  2989.  
  2990. void Semantic::CheckInheritedMethodThrows(AstMethodDeclaration *method_declaration, MethodSymbol *method)
  2991. {
  2992.     if (method_declaration -> NumThrows() > 0)
  2993.     {
  2994.         if (! method -> IsTyped())
  2995.             method -> ProcessMethodSignature((Semantic *) this, method_declaration -> Throw(0) -> LeftToken());
  2996.         method -> ProcessMethodThrows((Semantic *) this, method_declaration -> Throw(0) -> LeftToken());
  2997.  
  2998.         for (int i = 0; i < method_declaration -> NumThrows(); i++)
  2999.         {
  3000.             AstExpression *name = method_declaration -> Throw(i);
  3001.             TypeSymbol *exception = (TypeSymbol *) name -> symbol;
  3002.  
  3003.             if (CheckedException(exception))
  3004.             {
  3005.                 int k;
  3006.                 for (k = method -> NumThrows() - 1; k >= 0; k--)
  3007.                 {
  3008.                     if (exception -> IsSubclass(method -> Throws(k)))
  3009.                         break;
  3010.                 }
  3011.  
  3012.                 if (k < 0)
  3013.                 {
  3014.                     ReportSemError(SemanticError::MISMATCHED_OVERRIDDEN_EXCEPTION,
  3015.                                    name -> LeftToken(),
  3016.                                    name -> RightToken(),
  3017.                                    exception -> Name(),
  3018.                                    method -> Header(),
  3019.                                    method -> containing_type -> ContainingPackage() -> PackageName(),
  3020.                                    method -> containing_type -> ExternalName());
  3021.                 }
  3022.             }
  3023.         }
  3024.     }
  3025.  
  3026.     return;
  3027. }
  3028.  
  3029.  
  3030. void Semantic::CheckMethodOverride(AstMethodDeclaration *method_declaration, MethodSymbol *hidden_method)
  3031. {
  3032.     AstMethodDeclarator *method_declarator = method_declaration -> method_declarator;
  3033.     MethodSymbol *method = method_declaration -> method_symbol;
  3034.  
  3035.     if (hidden_method -> Type() != method -> Type())
  3036.     {
  3037.         ReportSemError(SemanticError::MISMATCHED_INHERITED_METHOD,
  3038.                        method_declarator -> LeftToken(),
  3039.                        method_declarator -> RightToken(),
  3040.                        method -> Header(),
  3041.                        hidden_method -> Header(),
  3042.                        hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3043.                        hidden_method -> containing_type -> ExternalName());
  3044.     }
  3045.     if (hidden_method -> IsFinal() || hidden_method -> ACC_PRIVATE()) // Merged because same kind of message. See error.cpp
  3046.         ReportSemError(hidden_method -> IsFinal() ? SemanticError::FINAL_METHOD_OVERRIDE : SemanticError::PRIVATE_METHOD_OVERRIDE,
  3047.                        method_declarator -> LeftToken(),
  3048.                        method_declarator -> RightToken(),
  3049.                        method -> Header(),
  3050.                        hidden_method -> Header(),
  3051.                        hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3052.                        hidden_method -> containing_type -> ExternalName());
  3053.  
  3054.     if (method -> ACC_STATIC() != hidden_method -> ACC_STATIC())
  3055.     {
  3056.         if (method -> ACC_STATIC())
  3057.              ReportSemError(SemanticError::INSTANCE_METHOD_OVERRIDE,
  3058.                             method_declarator -> LeftToken(),
  3059.                             method_declarator -> RightToken(),
  3060.                             method -> Header(),
  3061.                             hidden_method -> Header(),
  3062.                             hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3063.                             hidden_method -> containing_type -> ExternalName());
  3064.         else ReportSemError(SemanticError::CLASS_METHOD_OVERRIDE,
  3065.                             method_declarator -> LeftToken(),
  3066.                             method_declarator -> RightToken(),
  3067.                             method -> Header(),
  3068.                             hidden_method -> Header(),
  3069.                             hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3070.                             hidden_method -> containing_type -> ExternalName());
  3071.     }
  3072.  
  3073.     if (hidden_method -> ACC_PUBLIC())
  3074.     {
  3075.         if (! method -> ACC_PUBLIC())
  3076.             ReportSemError(SemanticError::BAD_ACCESS_METHOD_OVERRIDE,
  3077.                            method_declarator -> LeftToken(),
  3078.                            method_declarator -> RightToken(),
  3079.                            method -> Header(),
  3080.                            (method -> ACC_PRIVATE() ? StringConstant::US_private : (method -> ACC_PROTECTED() ? StringConstant::US_protected : StringConstant::US_default)),
  3081.                            hidden_method -> Header(),
  3082.                            StringConstant::US_public,
  3083.                            hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3084.                            hidden_method -> containing_type -> ExternalName());
  3085.     }
  3086.     else if (hidden_method -> ACC_PROTECTED())
  3087.     {
  3088.         if (! (method -> ACC_PROTECTED() || method -> ACC_PUBLIC()))
  3089.              ReportSemError(SemanticError::BAD_ACCESS_METHOD_OVERRIDE,
  3090.                             method_declarator -> LeftToken(),
  3091.                             method_declarator -> RightToken(),
  3092.                             method -> Header(),
  3093.                             StringConstant::US_default,
  3094.                             hidden_method -> Header(),
  3095.                             StringConstant::US_protected,
  3096.                             hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3097.                             hidden_method -> containing_type -> ExternalName());
  3098.     }
  3099.     else if (method -> ACC_PRIVATE()) // The hidden_method method must have default access as it cannot be private...
  3100.     {
  3101.          ReportSemError(SemanticError::BAD_ACCESS_METHOD_OVERRIDE,
  3102.                         method_declarator -> LeftToken(),
  3103.                         method_declarator -> RightToken(),
  3104.                         method -> Header(),
  3105.                         StringConstant::US_private,
  3106.                         hidden_method -> Header(),
  3107.                         StringConstant::US_default,
  3108.                         hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3109.                         hidden_method -> containing_type -> ExternalName());
  3110.     }
  3111.  
  3112.     CheckInheritedMethodThrows(method_declaration, hidden_method);
  3113.  
  3114.     return;
  3115. }
  3116.  
  3117.  
  3118. void Semantic::CheckInheritedMethodThrows(AstClassDeclaration *class_declaration, MethodSymbol *method, MethodSymbol *hidden_method)
  3119. {
  3120.     method -> ProcessMethodThrows((Semantic *) this, class_declaration -> identifier_token);
  3121.     hidden_method -> ProcessMethodThrows((Semantic *) this, class_declaration -> identifier_token);
  3122.  
  3123.     for (int i = method -> NumThrows() - 1; i >= 0; i--)
  3124.     {
  3125.         TypeSymbol *exception = method -> Throws(i);
  3126.  
  3127.         if (CheckedException(exception))
  3128.         {
  3129.             int k;
  3130.             for (k = hidden_method -> NumThrows() - 1; k >= 0; k--)
  3131.             {
  3132.                 if (exception -> IsSubclass(hidden_method -> Throws(k)))
  3133.                     break;
  3134.             }
  3135.  
  3136.             if (k < 0)
  3137.             {
  3138.                 if (! method -> IsTyped())
  3139.                     method -> ProcessMethodSignature((Semantic *) this, class_declaration -> identifier_token);
  3140.  
  3141.                 if (! hidden_method -> IsTyped())
  3142.                     hidden_method -> ProcessMethodSignature((Semantic *) this, class_declaration -> identifier_token);
  3143.  
  3144.                 ReportSemError(SemanticError::MISMATCHED_OVERRIDDEN_EXCEPTION_EXTERNALLY,
  3145.                                class_declaration -> identifier_token,
  3146.                                (class_declaration -> NumInterfaces() > 0
  3147.                                      ? class_declaration -> Interface(class_declaration -> NumInterfaces() - 1) -> RightToken()
  3148.                                      : (class_declaration -> super_opt ? class_declaration -> super_opt -> RightToken()
  3149.                                                                        : class_declaration -> identifier_token)),
  3150.                                lex_stream -> NameString(class_declaration -> identifier_token),
  3151.                                exception -> Name(),
  3152.                                method -> Header(),
  3153.                                hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3154.                                hidden_method -> containing_type -> ExternalName(),
  3155.                                hidden_method -> Header(),
  3156.                                method -> containing_type -> ContainingPackage() -> PackageName(),
  3157.                                method -> containing_type -> ExternalName());
  3158.             }
  3159.         }
  3160.     }
  3161.  
  3162.     return;
  3163. }
  3164.  
  3165.  
  3166. void Semantic::CheckMethodOverride(AstClassDeclaration *class_declaration, MethodSymbol *method, MethodSymbol *hidden_method)
  3167. {
  3168.     if (hidden_method -> Type() != method -> Type())
  3169.         ReportSemError(SemanticError::MISMATCHED_INHERITED_METHOD_EXTERNALLY,
  3170.                        class_declaration -> identifier_token,
  3171.                        (class_declaration -> NumInterfaces() > 0
  3172.                                            ? class_declaration -> Interface(class_declaration -> NumInterfaces() - 1) -> RightToken()
  3173.                                            : (class_declaration -> super_opt ? class_declaration -> super_opt -> RightToken()
  3174.                                                                              : class_declaration -> identifier_token)),
  3175.                        lex_stream -> NameString(class_declaration -> identifier_token),
  3176.                        method -> Header(),
  3177.                        method -> containing_type -> ContainingPackage() -> PackageName(),
  3178.                        method -> containing_type -> ExternalName(),
  3179.                        hidden_method -> Header(),
  3180.                        hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3181.                        hidden_method -> containing_type -> ExternalName());
  3182.     if (hidden_method -> IsFinal() || hidden_method -> ACC_PRIVATE()) // Merged because same kind of message. See error.cpp
  3183.         ReportSemError(hidden_method -> IsFinal() ? SemanticError::FINAL_METHOD_OVERRIDE_EXTERNALLY
  3184.                                                   : SemanticError::PRIVATE_METHOD_OVERRIDE_EXTERNALLY,
  3185.                        class_declaration -> identifier_token,
  3186.                        (class_declaration -> NumInterfaces() > 0
  3187.                                            ? class_declaration -> Interface(class_declaration -> NumInterfaces() - 1) -> RightToken()
  3188.                                            : (class_declaration -> super_opt ? class_declaration -> super_opt -> RightToken()
  3189.                                                                              : class_declaration -> identifier_token)),
  3190.                        lex_stream -> NameString(class_declaration -> identifier_token),
  3191.                        method -> Header(),
  3192.                        method -> containing_type -> ContainingPackage() -> PackageName(),
  3193.                        method -> containing_type -> ExternalName(),
  3194.                        hidden_method -> Header(),
  3195.                        hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3196.                        hidden_method -> containing_type -> ExternalName());
  3197.  
  3198.     if (method -> ACC_STATIC() != hidden_method -> ACC_STATIC())
  3199.     {
  3200.         if (method -> ACC_STATIC())
  3201.              ReportSemError(SemanticError::INSTANCE_METHOD_OVERRIDE_EXTERNALLY,
  3202.                             class_declaration -> identifier_token,
  3203.                             (class_declaration -> NumInterfaces() > 0
  3204.                                                 ? class_declaration -> Interface(class_declaration -> NumInterfaces() - 1) -> RightToken()
  3205.                                                 : (class_declaration -> super_opt ? class_declaration -> super_opt -> RightToken()
  3206.                                                                                   : class_declaration -> identifier_token)),
  3207.                             lex_stream -> NameString(class_declaration -> identifier_token),
  3208.                             method -> Header(),
  3209.                             method -> containing_type -> ContainingPackage() -> PackageName(),
  3210.                             method -> containing_type -> ExternalName(),
  3211.                             hidden_method -> Header(),
  3212.                             hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3213.                             hidden_method -> containing_type -> ExternalName());
  3214.         else ReportSemError(SemanticError::CLASS_METHOD_OVERRIDE_EXTERNALLY,
  3215.                             class_declaration -> identifier_token,
  3216.                             (class_declaration -> NumInterfaces() > 0
  3217.                                                 ? class_declaration -> Interface(class_declaration -> NumInterfaces() - 1) -> RightToken()
  3218.                                                 : (class_declaration -> super_opt ? class_declaration -> super_opt -> RightToken()
  3219.                                                                                   : class_declaration -> identifier_token)),
  3220.                             lex_stream -> NameString(class_declaration -> identifier_token),
  3221.                             method -> Header(),
  3222.                             method -> containing_type -> ContainingPackage() -> PackageName(),
  3223.                             method -> containing_type -> ExternalName(),
  3224.                             hidden_method -> Header(),
  3225.                             hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3226.                             hidden_method -> containing_type -> ExternalName());
  3227.     }
  3228.  
  3229.     if (hidden_method -> ACC_PUBLIC())
  3230.     {
  3231.         if (! method -> ACC_PUBLIC())
  3232.             ReportSemError(SemanticError::BAD_ACCESS_METHOD_OVERRIDE_EXTERNALLY,
  3233.                            class_declaration -> identifier_token,
  3234.                            (class_declaration -> NumInterfaces() > 0
  3235.                                                ? class_declaration -> Interface(class_declaration -> NumInterfaces() - 1) -> RightToken()
  3236.                                                : (class_declaration -> super_opt ? class_declaration -> super_opt -> RightToken()
  3237.                                                                                  : class_declaration -> identifier_token)),
  3238.                            lex_stream -> NameString(class_declaration -> identifier_token),
  3239.                            method -> Header(),
  3240.                            (method -> ACC_PRIVATE() ? StringConstant::US_private
  3241.                                                     : (method -> ACC_PROTECTED() ? StringConstant::US_protected
  3242.                                                                                  : StringConstant::US_default)),
  3243.                            method -> containing_type -> ContainingPackage() -> PackageName(),
  3244.                            method -> containing_type -> ExternalName(),
  3245.                            hidden_method -> Header(),
  3246.                            StringConstant::US_public,
  3247.                            hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3248.                            hidden_method -> containing_type -> ExternalName());
  3249.     }
  3250.     else if (hidden_method -> ACC_PROTECTED())
  3251.     {
  3252.         if (! (method -> ACC_PROTECTED() || method -> ACC_PUBLIC()))
  3253.              ReportSemError(SemanticError::BAD_ACCESS_METHOD_OVERRIDE,
  3254.                             class_declaration -> identifier_token,
  3255.                             (class_declaration -> NumInterfaces() > 0
  3256.                                                 ? class_declaration -> Interface(class_declaration -> NumInterfaces() - 1) -> RightToken()
  3257.                                                 : (class_declaration -> super_opt ? class_declaration -> super_opt -> RightToken()
  3258.                                                                                   : class_declaration -> identifier_token)),
  3259.                             lex_stream -> NameString(class_declaration -> identifier_token),
  3260.                             method -> Header(),
  3261.                             StringConstant::US_default,
  3262.                             method -> containing_type -> ContainingPackage() -> PackageName(),
  3263.                             method -> containing_type -> ExternalName(),
  3264.                             hidden_method -> Header(),
  3265.                             StringConstant::US_protected,
  3266.                             hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3267.                             hidden_method -> containing_type -> ExternalName());
  3268.     }
  3269.     else if (method -> ACC_PRIVATE()) // The hidden_method method must have default access as it cannot be private...
  3270.     {
  3271.          ReportSemError(SemanticError::BAD_ACCESS_METHOD_OVERRIDE_EXTERNALLY,
  3272.                         class_declaration -> identifier_token,
  3273.                         (class_declaration -> NumInterfaces() > 0
  3274.                                             ? class_declaration -> Interface(class_declaration -> NumInterfaces() - 1) -> RightToken()
  3275.                                             : (class_declaration -> super_opt ? class_declaration -> super_opt -> RightToken()
  3276.                                                                               : class_declaration -> identifier_token)),
  3277.                         lex_stream -> NameString(class_declaration -> identifier_token),
  3278.                         method -> Header(),
  3279.                         StringConstant::US_private,
  3280.                         method -> containing_type -> ContainingPackage() -> PackageName(),
  3281.                         method -> containing_type -> ExternalName(),
  3282.                         hidden_method -> Header(),
  3283.                         StringConstant::US_default,
  3284.                         hidden_method -> containing_type -> ContainingPackage() -> PackageName(),
  3285.                         hidden_method -> containing_type -> ExternalName());
  3286.     }
  3287.  
  3288.     CheckInheritedMethodThrows(class_declaration, method, hidden_method);
  3289.  
  3290.     return;
  3291. }
  3292.  
  3293.  
  3294. void Semantic::AddInheritedTypes(TypeSymbol *base_type, TypeSymbol *super_type)
  3295. {
  3296.     ExpandedTypeTable &base_expanded_table = *(base_type -> expanded_type_table),
  3297.                       &super_expanded_table = *(super_type -> expanded_type_table);
  3298.  
  3299.     for (int j = 0; j < super_expanded_table.symbol_pool.Length(); j++)
  3300.     {
  3301.         TypeShadowSymbol *type_shadow_symbol = super_expanded_table.symbol_pool[j];
  3302.         TypeSymbol *type_symbol = type_shadow_symbol -> type_symbol;
  3303.  
  3304.         //
  3305.         // Note that since all fields in an interface are implicitly public, all other fields
  3306.         // encountered here are enclosed in a type that is a super class of base_type.
  3307.         //
  3308.         if (type_symbol -> ACC_PUBLIC() ||
  3309.             type_symbol -> ACC_PROTECTED() ||
  3310.             ((! type_symbol -> ACC_PRIVATE()) &&
  3311.              (super_type -> ContainingPackage() == base_type -> ContainingPackage())))
  3312.         {
  3313.             NameSymbol *name_symbol = type_symbol -> Identity();
  3314.             TypeShadowSymbol *shadow = base_expanded_table.FindTypeShadowSymbol(name_symbol);
  3315.  
  3316.             if ((! shadow) || (shadow -> type_symbol -> owner != base_type))
  3317.             {
  3318.                 if (! shadow)
  3319.                      shadow = base_expanded_table.InsertTypeShadowSymbol(type_symbol);
  3320.                 else shadow -> AddConflict(type_symbol);
  3321.  
  3322.                 if (type_symbol -> owner != super_type) // main type doesn't override all other fields? process conflicts.
  3323.                 {
  3324.                     for (int k = 0; k < type_shadow_symbol -> NumConflicts(); k++)
  3325.                         shadow -> AddConflict(type_shadow_symbol -> Conflict(k));
  3326.                 }
  3327.             }
  3328.             //
  3329.             // TODO: maybe? if base_type is a nested type check if a type with the same
  3330.             //       name appears in one of the enclosed lexical scopes. If so, add
  3331.             //       it to the shadow!
  3332.             //
  3333.         }
  3334.     }
  3335.  
  3336.     return;
  3337. }
  3338.  
  3339.  
  3340. void Semantic::AddInheritedFields(TypeSymbol *base_type, TypeSymbol *super_type)
  3341. {
  3342.     ExpandedFieldTable &base_expanded_table = *(base_type -> expanded_field_table),
  3343.                        &super_expanded_table = *(super_type -> expanded_field_table);
  3344.  
  3345.     for (int i = 0; i < super_expanded_table.symbol_pool.Length(); i++)
  3346.     {
  3347.         VariableShadowSymbol *variable_shadow_symbol = super_expanded_table.symbol_pool[i];
  3348.         VariableSymbol *variable_symbol = variable_shadow_symbol -> variable_symbol;
  3349.         //
  3350.         // Note that since all fields in an interface are implicitly public, all other fields
  3351.         // encountered here are enclosed in a type that is a super class of base_type.
  3352.         //
  3353.         if (variable_symbol -> ACC_PUBLIC() ||
  3354.             variable_symbol -> ACC_PROTECTED() ||
  3355.             ((! variable_symbol -> ACC_PRIVATE()) &&
  3356.              (super_type -> ContainingPackage() == base_type -> ContainingPackage())))
  3357.         {
  3358.             NameSymbol *name_symbol = variable_symbol -> Identity();
  3359.             VariableShadowSymbol *shadow = base_expanded_table.FindVariableShadowSymbol(name_symbol);
  3360.  
  3361.             if ((! shadow) || (shadow -> variable_symbol -> owner != base_type))
  3362.             {
  3363.                 if (! shadow)
  3364.                      shadow = base_expanded_table.InsertVariableShadowSymbol(variable_symbol);
  3365.                 else shadow -> AddConflict(variable_symbol);
  3366.  
  3367.                 if (variable_symbol -> owner != super_type) // main variable doesn't override all other fields? process conflicts.
  3368.                 {
  3369.                     for (int k = 0; k < variable_shadow_symbol -> NumConflicts(); k++)
  3370.                         shadow -> AddConflict(variable_shadow_symbol -> Conflict(k));
  3371.                 }
  3372.             }
  3373.         }
  3374.     }
  3375.  
  3376.     return;
  3377. }
  3378.  
  3379.  
  3380. void Semantic::AddInheritedMethods(TypeSymbol *base_type, TypeSymbol *super_type, LexStream::TokenIndex tok)
  3381. {
  3382.     ExpandedMethodTable &base_expanded_table = *(base_type -> expanded_method_table),
  3383.                         &super_expanded_table = *(super_type -> expanded_method_table);
  3384.  
  3385.     for (int k = 0; k < super_expanded_table.symbol_pool.Length(); k++)
  3386.     {
  3387.         MethodShadowSymbol *method_shadow_symbol = super_expanded_table.symbol_pool[k];
  3388.         MethodSymbol *method = method_shadow_symbol -> method_symbol;
  3389.  
  3390.         //
  3391.         // Note that since all methods in an interface are implicitly
  3392.         // public, all other methods encountered here are enclosed in a
  3393.         // type that is a super class of base_type.
  3394.         //
  3395.         if (method -> ACC_PUBLIC() ||
  3396.             method -> ACC_PROTECTED() ||
  3397.             ((! method -> ACC_PRIVATE()) &&
  3398.              (super_type -> ContainingPackage() == base_type -> ContainingPackage())))
  3399.         {
  3400.             MethodShadowSymbol *base_method_shadow =
  3401.                   base_expanded_table.FindMethodShadowSymbol(method -> Identity());
  3402.             if (! base_method_shadow)
  3403.                  base_expanded_table.InsertMethodShadowSymbol(method);
  3404.             else
  3405.             {
  3406.                 MethodShadowSymbol *shadow = base_expanded_table.FindOverloadMethodShadow(method, (Semantic *) this, tok);
  3407.  
  3408.                 if (! shadow)
  3409.                      base_expanded_table.Overload(base_method_shadow, method);
  3410.                 else
  3411.                 {
  3412.                     shadow -> AddConflict(method);
  3413.  
  3414.                     //
  3415.                     // If main method in question does not override all other methods,
  3416.                     // add all other conflicting methods.
  3417.                     //
  3418.                     if (method -> containing_type != super_type)
  3419.                     {
  3420.                         for (int i = 0; i < method_shadow_symbol -> NumConflicts(); i++)
  3421.                             shadow -> AddConflict(method_shadow_symbol -> Conflict(i));
  3422.                     }
  3423.                 }
  3424.             }
  3425.         }
  3426.         else if (! (method -> ACC_PRIVATE() || method -> IsSynthetic())) // Not a method with default access from another package?
  3427.         {
  3428.             MethodShadowSymbol *base_method_shadow = base_expanded_table.FindMethodShadowSymbol(method -> Identity());
  3429.  
  3430.             if (base_method_shadow)
  3431.             {
  3432.                 MethodShadowSymbol *shadow = base_expanded_table.FindOverloadMethodShadow(method, (Semantic *) this, tok);
  3433.  
  3434.                 if (shadow)
  3435.                 {
  3436.                     LexStream::TokenIndex left_tok,
  3437.                                           right_tok;
  3438.  
  3439.                     if (ThisType() == base_type)
  3440.                     {
  3441.                         AstMethodDeclaration *method_declaration = (AstMethodDeclaration *)
  3442.                                                                     shadow -> method_symbol -> method_or_constructor_declaration;
  3443.                         AstMethodDeclarator *method_declarator = method_declaration -> method_declarator;
  3444.  
  3445.                         left_tok = method_declarator -> LeftToken();
  3446.                         right_tok = method_declarator -> RightToken();
  3447.                     }
  3448.                     else
  3449.                     {
  3450.                         AstInterfaceDeclaration *interface_declaration = ThisType() -> declaration -> InterfaceDeclarationCast();
  3451.                         AstClassDeclaration *class_declaration = ThisType() -> declaration -> ClassDeclarationCast();
  3452.                         if (interface_declaration)
  3453.                         {
  3454.                             left_tok = right_tok = interface_declaration -> identifier_token;
  3455.                         }
  3456.                         else if (class_declaration)
  3457.                         {
  3458.                             left_tok = right_tok = class_declaration -> identifier_token;
  3459.                         }
  3460.                         else
  3461.                         {
  3462.                             AstClassInstanceCreationExpression *class_creation = ThisType() -> declaration
  3463.                                                                                             -> ClassInstanceCreationExpressionCast();
  3464.  
  3465.                             assert(class_creation);
  3466.  
  3467.                             left_tok = class_creation -> class_type -> LeftToken();
  3468.                             right_tok = class_creation -> class_type -> RightToken();
  3469.                         }
  3470.                     }
  3471.  
  3472.                     if (! method -> IsTyped())
  3473.                         method -> ProcessMethodSignature((Semantic *) this, tok);
  3474.  
  3475.                     ReportSemError(SemanticError::DEFAULT_METHOD_NOT_OVERRIDDEN,
  3476.                                    left_tok,
  3477.                                    right_tok,
  3478.                                    method -> Header(),
  3479.                                    base_type -> ContainingPackage() -> PackageName(),
  3480.                                    base_type -> ExternalName(),
  3481.                                    super_type -> ContainingPackage() -> PackageName(),
  3482.                                    super_type -> ExternalName());
  3483.                 }
  3484.             }
  3485.         }
  3486.     }
  3487.  
  3488.     return;
  3489. }
  3490.  
  3491.  
  3492. void Semantic::ComputeTypesClosure(TypeSymbol *type, LexStream::TokenIndex tok)
  3493. {
  3494.     type -> expanded_type_table = new ExpandedTypeTable();
  3495.  
  3496.     TypeSymbol *super_class = type -> super;
  3497.     if (super_class)
  3498.     {
  3499.         if (! super_class -> expanded_type_table)
  3500.             ComputeTypesClosure(super_class, tok);
  3501.     }
  3502.  
  3503.     for (int j = 0; j < type -> NumInterfaces(); j++)
  3504.     {
  3505.         TypeSymbol *interf = type -> Interface(j);
  3506.         if (! interf -> expanded_type_table)
  3507.             ComputeTypesClosure(interf, tok);
  3508.     }
  3509.  
  3510.     if (! type -> NestedTypesProcessed())
  3511.         type -> ProcessNestedTypeSignatures((Semantic *) this, tok);
  3512.     for (int i = 0; i < type -> NumTypeSymbols(); i++)
  3513.     {
  3514.         if (! type -> TypeSym(i) -> Bad())
  3515.             type -> expanded_type_table -> InsertTypeShadowSymbol(type -> TypeSym(i));
  3516.     }
  3517.     if (super_class)
  3518.         AddInheritedTypes(type, super_class);
  3519.     for (int k = 0; k < type -> NumInterfaces(); k++)
  3520.         AddInheritedTypes(type, type -> Interface(k));
  3521.     type -> expanded_type_table -> CompressSpace();
  3522.  
  3523.     return;
  3524. }
  3525.  
  3526.  
  3527. void Semantic::ComputeFieldsClosure(TypeSymbol *type, LexStream::TokenIndex tok)
  3528. {
  3529.     type -> expanded_field_table = new ExpandedFieldTable();
  3530.  
  3531.     TypeSymbol *super_class = type -> super;
  3532.     if (super_class)
  3533.     {
  3534.         if (! super_class -> expanded_field_table)
  3535.             ComputeFieldsClosure(super_class, tok);
  3536.     }
  3537.  
  3538.     for (int j = 0; j < type -> NumInterfaces(); j++)
  3539.     {
  3540.         TypeSymbol *interf = type -> Interface(j);
  3541.         if (! interf -> expanded_field_table)
  3542.             ComputeFieldsClosure(interf, tok);
  3543.     }
  3544.  
  3545.     assert(type -> FieldMembersProcessed());
  3546.  
  3547.     for (int i = 0; i < type -> NumVariableSymbols(); i++)
  3548.     {
  3549.         VariableSymbol *variable = type -> VariableSym(i);
  3550.         type -> expanded_field_table -> InsertVariableShadowSymbol(variable);
  3551.     }
  3552.  
  3553.     //
  3554.     // As the type Object which is the super type of all interfaces does
  3555.     // not contain any field declarations, we don't have to do any special
  3556.     // check here as we have to when computing method closures.
  3557.     //
  3558.     if (super_class)
  3559.         AddInheritedFields(type, super_class);
  3560.     for (int k = 0; k < type -> NumInterfaces(); k++)
  3561.         AddInheritedFields(type, type -> Interface(k));
  3562.     type -> expanded_field_table -> CompressSpace();
  3563.  
  3564.     return;
  3565. }
  3566.  
  3567.  
  3568. void Semantic::ComputeMethodsClosure(TypeSymbol *type, LexStream::TokenIndex tok)
  3569. {
  3570.     type -> expanded_method_table = new ExpandedMethodTable();
  3571.  
  3572.     TypeSymbol *super_class = type -> super;
  3573.     if (super_class)
  3574.     {
  3575.         if (! super_class -> expanded_method_table)
  3576.             ComputeMethodsClosure(super_class, tok);
  3577.     }
  3578.  
  3579.     for (int j = 0; j < type -> NumInterfaces(); j++)
  3580.     {
  3581.         TypeSymbol *interf = type -> Interface(j);
  3582.  
  3583.         if (! interf -> expanded_method_table)
  3584.             ComputeMethodsClosure(interf, tok);
  3585.     }
  3586.  
  3587.     assert(type -> MethodMembersProcessed());
  3588.  
  3589.     for (int i = 0; i < type -> NumMethodSymbols(); i++)
  3590.     {
  3591.         MethodSymbol *method = type -> MethodSym(i);
  3592.         //
  3593.         // If the method in question is neither a constructor nor an
  3594.         // initializer, then ...
  3595.         //        if (method -> Identity() != control.init_name_symbol &&
  3596.         //            method -> Identity() != control.block_init_name_symbol &&
  3597.         //            method -> Identity() != control.clinit_name_symbol)
  3598.         //
  3599.         if (*(method -> Name()) != U_LESS)
  3600.         {
  3601.             type -> expanded_method_table -> Overload(method);
  3602.         }
  3603.     }
  3604.     if (super_class && (! type -> ACC_INTERFACE()))
  3605.         AddInheritedMethods(type, super_class, tok);
  3606.     for (int k = 0; k < type -> NumInterfaces(); k++)
  3607.         AddInheritedMethods(type, type -> Interface(k), tok);
  3608.     if (type -> ACC_INTERFACE()) // the super class is Object
  3609.         AddInheritedMethods(type, control.Object(), tok);
  3610.     type -> expanded_method_table -> CompressSpace();
  3611.  
  3612.     return;
  3613. }
  3614.  
  3615.  
  3616. void Semantic::ProcessFormalParameters(BlockSymbol *block, AstMethodDeclarator *method_declarator)
  3617. {
  3618.     for (int i = 0; i < method_declarator -> NumFormalParameters(); i++)
  3619.     {
  3620.         AstFormalParameter *parameter = method_declarator -> FormalParameter(i);
  3621.         AstArrayType *array_type = parameter -> type -> ArrayTypeCast();
  3622.         Ast *actual_type = (array_type ? array_type -> type : parameter -> type);
  3623.  
  3624.         AccessFlags access_flags = ProcessFormalModifiers(parameter);
  3625.  
  3626.         AstPrimitiveType *primitive_type = actual_type -> PrimitiveTypeCast();
  3627.         TypeSymbol *parm_type = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(actual_type));
  3628.  
  3629.         AstVariableDeclaratorId *name = parameter -> formal_declarator -> variable_declarator_name;
  3630.         NameSymbol *name_symbol = lex_stream -> NameSymbol(name -> identifier_token);
  3631.         VariableSymbol *symbol = block -> FindVariableSymbol(name_symbol);
  3632.         if (symbol)
  3633.         {
  3634.             ReportSemError(SemanticError::DUPLICATE_FORMAL_PARAMETER,
  3635.                            name -> identifier_token,
  3636.                            name -> identifier_token,
  3637.                            name_symbol -> Name());
  3638.         }
  3639.         else symbol = block -> InsertVariableSymbol(name_symbol);
  3640.  
  3641.         int num_dimensions = (array_type ? array_type -> NumBrackets() : 0) + name -> NumBrackets();
  3642.         if (num_dimensions == 0)
  3643.              symbol -> SetType(parm_type);
  3644.         else symbol -> SetType(parm_type -> GetArrayType((Semantic *) this, num_dimensions));
  3645.         symbol -> SetFlags(access_flags);
  3646.         symbol -> MarkComplete();
  3647.  
  3648.         parameter -> formal_declarator -> symbol = symbol;
  3649.     }
  3650.  
  3651.     return;
  3652. }
  3653.  
  3654.  
  3655. void Semantic::ProcessMethodDeclaration(AstMethodDeclaration *method_declaration)
  3656. {
  3657.     TypeSymbol *this_type = ThisType();
  3658.     AccessFlags access_flags = (this_type -> ACC_INTERFACE()
  3659.                                            ? ProcessAbstractMethodModifiers(method_declaration)
  3660.                                            : ProcessMethodModifiers(method_declaration));
  3661.  
  3662.     //
  3663.     // TODO: File Query Sun on that one. We no longer explicitly mark such methods as final
  3664.     //       as it appears that some tools expect these methods to remain unmarked.
  3665.     //
  3666.     //
  3667.     // A private method and all methods declared in a final class are implicitly final.
  3668.     //
  3669.     // if (access_flags.ACC_PRIVATE() || this_type -> ACC_FINAL())
  3670.     //    access_flags.SetACC_FINAL();
  3671.     //
  3672.  
  3673.     //
  3674.     // A method enclosed in an inner type may not be declared static.
  3675.     //
  3676.     if (access_flags.ACC_STATIC() && this_type -> IsInner())
  3677.     {
  3678.         AstModifier *modifier = NULL;
  3679.         for (int i = 0; i < method_declaration -> NumMethodModifiers(); i++)
  3680.         {
  3681.             if (method_declaration -> MethodModifier(i) -> kind == Ast::STATIC)
  3682.                 modifier = method_declaration -> MethodModifier(i);
  3683.         }
  3684.  
  3685.         assert(modifier);
  3686.  
  3687.         ReportSemError(SemanticError::STATIC_METHOD_IN_INNER_CLASS,
  3688.                        modifier -> modifier_kind_token,
  3689.                        modifier -> modifier_kind_token);
  3690.     }
  3691.  
  3692.     //
  3693.     //
  3694.     //
  3695.     AstArrayType *array_type = method_declaration -> type -> ArrayTypeCast();
  3696.     Ast *actual_type = (array_type ? array_type -> type : method_declaration -> type);
  3697.     AstPrimitiveType *primitive_type = actual_type -> PrimitiveTypeCast();
  3698.     TypeSymbol *method_type = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(actual_type));
  3699.  
  3700.     AstMethodDeclarator *method_declarator = method_declaration -> method_declarator;
  3701.     if (method_declarator -> NumBrackets() > 0)
  3702.     {
  3703.         if (method_type == control.void_type)
  3704.              ReportSemError(SemanticError::VOID_ARRAY,
  3705.                             method_declaration -> type -> LeftToken(),
  3706.                             method_declarator -> RightToken());
  3707.         else ReportSemError(SemanticError::OBSOLESCENT_BRACKETS,
  3708.                             method_declarator -> LeftToken(),
  3709.                             method_declarator -> RightToken());
  3710.     }
  3711.  
  3712.     NameSymbol *name_symbol = lex_stream -> NameSymbol(method_declarator -> identifier_token);
  3713.  
  3714.     if (name_symbol == this_type -> Identity())
  3715.     {
  3716.         ReportSemError(SemanticError::METHOD_WITH_CONSTRUCTOR_NAME,
  3717.                        method_declaration -> type -> LeftToken(),
  3718.                        method_declarator -> identifier_token,
  3719.                        name_symbol -> Name());
  3720.     }
  3721.  
  3722.     //
  3723.     // As the body of the method may not have been parsed yet, we estimate a size
  3724.     // for its symbol table based on the number of lines in the body + a margin for one-liners.
  3725.     //
  3726.     BlockSymbol *block_symbol = new BlockSymbol(method_declarator -> NumFormalParameters());
  3727.     block_symbol -> max_variable_index = (access_flags.ACC_STATIC() ? 0 : 1);
  3728.     ProcessFormalParameters(block_symbol, method_declarator);
  3729.  
  3730.     MethodSymbol *method = this_type -> FindMethodSymbol(name_symbol);
  3731.  
  3732.     if (! method)
  3733.         method = this_type -> InsertMethodSymbol(name_symbol);
  3734.     else
  3735.     {
  3736.         if (this_type -> FindOverloadMethod(method, method_declarator))
  3737.         {
  3738.             ReportSemError(SemanticError::DUPLICATE_METHOD,
  3739.                            method_declarator -> LeftToken(),
  3740.                            method_declarator -> RightToken(),
  3741.                            name_symbol -> Name(),
  3742.                            this_type -> Name());
  3743.             delete block_symbol;
  3744.             return;
  3745.         }
  3746.  
  3747.         method = this_type -> Overload(method);
  3748.     }
  3749.  
  3750.     int num_dimensions = (method_type == control.void_type
  3751.                                        ? 0
  3752.                                        : (array_type ? array_type -> NumBrackets() : 0) + method_declarator -> NumBrackets());
  3753.     if (num_dimensions == 0)
  3754.          method -> SetType(method_type);
  3755.     else method -> SetType(method_type -> GetArrayType((Semantic *) this, num_dimensions));
  3756.  
  3757.     //
  3758.     // if the method is not static, leave a slot for the "this" pointer.
  3759.     //
  3760.     method -> SetFlags(access_flags);
  3761.     method -> SetContainingType(this_type);
  3762.     method -> SetBlockSymbol(block_symbol);
  3763.     method -> method_or_constructor_declaration = method_declaration;
  3764.     for (int i = 0; i < method_declarator -> NumFormalParameters(); i++)
  3765.     {
  3766.         AstVariableDeclarator *formal_declarator = method_declarator -> FormalParameter(i) -> formal_declarator;
  3767.         VariableSymbol *symbol = formal_declarator -> symbol;
  3768.  
  3769.         symbol -> SetOwner(method);
  3770.         symbol -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  3771.         if (control.IsDoubleWordType(symbol -> Type()))
  3772.             block_symbol -> max_variable_index++;
  3773.         symbol -> declarator = formal_declarator;
  3774.         method -> AddFormalParameter(symbol);
  3775.     }
  3776.     method -> SetSignature(control);
  3777.  
  3778.     for (int k = 0; k < method_declaration -> NumThrows(); k++)
  3779.     {
  3780.         AstExpression *throw_expression = method_declaration -> Throw(k);
  3781.         TypeSymbol *throw_type = MustFindType(throw_expression);
  3782.         throw_expression -> symbol = throw_type;
  3783.         method -> AddThrows(throw_type);
  3784.     }
  3785.  
  3786.     method_declaration -> method_symbol = method; // save for processing bodies later.
  3787.  
  3788.     if (method -> ACC_ABSTRACT() && (! this_type -> ACC_ABSTRACT()))
  3789.     {
  3790.         ReportSemError(SemanticError::NON_ABSTRACT_TYPE_CONTAINS_ABSTRACT_METHOD,
  3791.                        method_declaration -> LeftToken(),
  3792.                        method_declarator -> identifier_token,
  3793.                        name_symbol -> Name(),
  3794.                        this_type -> Name());
  3795.     }
  3796.  
  3797.     if (lex_stream -> IsDeprecated(lex_stream -> Previous(method_declaration -> LeftToken())))
  3798.         method -> MarkDeprecated();
  3799.  
  3800.     return;
  3801. }
  3802.  
  3803.  
  3804. //
  3805. // Search for simple identifier which is supposed to be a type.
  3806. // If it is not found, issue an error message.
  3807. //
  3808. TypeSymbol *Semantic::FindPrimitiveType(AstPrimitiveType *primitive_type)
  3809. {
  3810.     switch(primitive_type -> kind)
  3811.     {
  3812.         case Ast::INT:
  3813.              return control.int_type;
  3814.         case Ast::DOUBLE:
  3815.              return control.double_type;
  3816.         case Ast::CHAR:
  3817.              return control.char_type;
  3818.         case Ast::LONG:
  3819.              return control.long_type;
  3820.         case Ast::FLOAT:
  3821.              return control.float_type;
  3822.         case Ast::BYTE:
  3823.              return control.byte_type;
  3824.         case Ast::SHORT:
  3825.              return control.short_type;
  3826.         case Ast::BOOLEAN:
  3827.              return control.boolean_type;
  3828.         default:
  3829.              break;
  3830.     }
  3831.  
  3832.     return control.void_type;
  3833. }
  3834.  
  3835.  
  3836. TypeSymbol *Semantic::ImportType(LexStream::TokenIndex identifier_token, NameSymbol *name_symbol)
  3837. {
  3838.     TypeSymbol *type = NULL;
  3839.     PackageSymbol *package = NULL;
  3840.     FileSymbol *file_symbol = NULL;
  3841.  
  3842.     for (int i = 0; i < import_on_demand_packages.Length(); i++)
  3843.     {
  3844.         PackageSymbol *import_package = import_on_demand_packages[i] -> PackageCast();
  3845.  
  3846.         if (import_package)
  3847.         {
  3848.             FileSymbol *symbol = Control::GetFile(control, import_package, name_symbol);
  3849.             if (symbol)
  3850.             {
  3851.                 if (! package)
  3852.                 {
  3853.                     file_symbol = symbol;
  3854.                     package = import_package;
  3855.                 }
  3856.                 else
  3857.                 {
  3858.                     ReportSemError(SemanticError::DUPLICATE_ON_DEMAND_IMPORT,
  3859.                                    identifier_token,
  3860.                                    identifier_token,
  3861.                                    name_symbol -> Name(),
  3862.                                    package -> PackageName(),
  3863.                                    import_package -> PackageName());
  3864.                 }
  3865.             }
  3866.         }
  3867.         else
  3868.         {
  3869.             TypeSymbol *import_type = (TypeSymbol *) import_on_demand_packages[i];
  3870.  
  3871.             if (! import_type -> expanded_type_table)
  3872.                 ComputeTypesClosure(import_type, identifier_token);
  3873.             TypeShadowSymbol *type_shadow_symbol = import_type -> expanded_type_table -> FindTypeShadowSymbol(name_symbol);
  3874.             if (type_shadow_symbol)
  3875.                 type = FindTypeInShadow(type_shadow_symbol, identifier_token);
  3876.         }
  3877.     }
  3878.  
  3879.     if (! type)
  3880.     {
  3881.         if (package)
  3882.         {
  3883.             type = package -> FindTypeSymbol(name_symbol);
  3884.             if (! type)
  3885.                  type = ReadType(file_symbol, package, name_symbol, identifier_token);
  3886.             else if (type -> SourcePending())
  3887.                  control.ProcessHeaders(type -> file_symbol);
  3888.         }
  3889.     }
  3890.  
  3891.     if (type && (! (type -> ACC_PUBLIC() || type -> ContainingPackage() == this_package)))
  3892.         ReportTypeInaccessible(identifier_token, identifier_token, type);
  3893.  
  3894.     return type;
  3895. }
  3896.  
  3897.  
  3898. TypeSymbol *Semantic::FindType(LexStream::TokenIndex identifier_token)
  3899. {
  3900.     TypeSymbol *type;
  3901.  
  3902.     NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  3903.  
  3904.     SemanticEnvironment *env;
  3905.     for (env = state_stack.Top(); env; env = env -> previous)
  3906.     {
  3907.         type = env -> symbol_table.FindTypeSymbol(name_symbol);
  3908.         if (type)
  3909.             break;
  3910.  
  3911.         type = env -> Type();
  3912.         if (name_symbol == type -> Identity()) // Recall that a type may not have the same name as one of its enclosing types.
  3913.             break;
  3914.  
  3915.         if (! type -> expanded_type_table)
  3916.             ComputeTypesClosure(type, identifier_token);
  3917.         TypeShadowSymbol *type_shadow_symbol = type -> expanded_type_table -> FindTypeShadowSymbol(name_symbol);
  3918.         if (type_shadow_symbol)
  3919.         {
  3920.             type = FindTypeInShadow(type_shadow_symbol, identifier_token);
  3921.             break;
  3922.         }
  3923.     }
  3924.  
  3925.     if (env) // The type was found in some enclosing environment?
  3926.     {
  3927.         //
  3928.         // If the type is an inherited type, make sure that there is not a
  3929.         // type of the same name within an enclosing lexical scope.
  3930.         //
  3931.         if (type -> owner -> TypeCast() && type -> owner != env -> Type())
  3932.         {
  3933.             for (SemanticEnvironment *env2 = env -> previous; env2; env2 = env2 -> previous)
  3934.             {
  3935.                 TypeSymbol *outer_type = env2 -> symbol_table.FindTypeSymbol(name_symbol); // check local type
  3936.                 if (! outer_type) // if local type not found, check inner type...
  3937.                 {
  3938.                     if (! env2 -> Type() -> expanded_type_table)
  3939.                         ComputeTypesClosure(env2 -> Type(), identifier_token);
  3940.                     TypeShadowSymbol *type_shadow_symbol = env2 -> Type() -> expanded_type_table
  3941.                                                                           -> FindTypeShadowSymbol(name_symbol);
  3942.                     if (type_shadow_symbol)
  3943.                         outer_type = FindTypeInShadow(type_shadow_symbol, identifier_token);
  3944.                 }
  3945.  
  3946.                 //
  3947.                 // If a different type of the same name was found in an enclosing scope.
  3948.                 //
  3949.                 if (outer_type && outer_type != type)
  3950.                 {
  3951.                     MethodSymbol *method = outer_type -> owner -> MethodCast();
  3952.  
  3953.                     if (method)
  3954.                     {
  3955.                         ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_LOCAL,
  3956.                                        identifier_token,
  3957.                                        identifier_token,
  3958.                                        lex_stream -> NameString(identifier_token),
  3959.                                        env -> Type() -> ContainingPackage() -> PackageName(),
  3960.                                        env -> Type() -> ExternalName(),
  3961.                                        method -> Name());
  3962.                         break;
  3963.                     }
  3964.                     else
  3965.                     {
  3966.                         ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_MEMBER,
  3967.                                        identifier_token,
  3968.                                        identifier_token,
  3969.                                        lex_stream -> NameString(identifier_token),
  3970.                                        env -> Type() -> ContainingPackage() -> PackageName(),
  3971.                                        env -> Type() -> ExternalName(),
  3972.                                        env2 -> Type() -> ContainingPackage() -> PackageName(),
  3973.                                        env2 -> Type() -> ExternalName());
  3974.                         break;
  3975.                     }
  3976.                 }
  3977.             }
  3978.         }
  3979.  
  3980.         return type;
  3981.     }
  3982.  
  3983.     //
  3984.     // check whether or not the type is in the current compilation unit
  3985.     // either because it was declared as a class or interface or it was
  3986.     // imported by a single-type-import declaration.
  3987.     //
  3988.     for (int i = 0; i < single_type_imports.Length(); i++)
  3989.     {
  3990.         type = single_type_imports[i];
  3991.         if (name_symbol == type -> Identity())
  3992.             return type;
  3993.     }
  3994.  
  3995.     //
  3996.     // If a package was specified in this file (the one we are compiling),
  3997.     // we look for the type requested inside the package in question.
  3998.     // Otherwise, we look for the type requested in the unnamed package.
  3999.     // If we don't find the type or we find a bad type we check to see
  4000.     // if the type can be imported.
  4001.     //
  4002.     PackageSymbol *package = (compilation_unit -> package_declaration_opt ? this_package : control.unnamed_package);
  4003.     type = FindSimpleNameType(package, identifier_token);
  4004.     TypeSymbol *imported_type = ((! type ) || type -> Bad() ? ImportType(identifier_token, name_symbol) : (TypeSymbol *) NULL);
  4005.  
  4006.     //
  4007.     // If a valid type can be imported on demand, chose that type.
  4008.     // Otherwise, if a type was found at all, do some final checks on it.
  4009.     //
  4010.     // Note that a type T contained in a package P is always accessible to all other
  4011.     // types contained in P. I.e., we do not need to perform access check for type...
  4012.     //
  4013.     //
  4014.     if (imported_type)
  4015.         type = imported_type;
  4016.     else if (type)
  4017.     {
  4018.         //
  4019.         // If a type T was specified in a source file that is not called T.java
  4020.         // but X.java (where X != T) and we are not currently compiling file X,
  4021.         // issue a warning to alert the user that in some circumstances, this
  4022.         // may not be visible. (i.e., if the file X has not yet been compiled,
  4023.         // then T is invisile as the compiler will only look for T in T.java.)
  4024.         //
  4025.         FileSymbol *file_symbol = type -> file_symbol;
  4026.         if (file_symbol && (type -> Identity() != file_symbol -> Identity()) && (file_symbol != this -> source_file_symbol))
  4027.         {
  4028.             ReportSemError(SemanticError::REFERENCE_TO_TYPE_IN_MISMATCHED_FILE,
  4029.                            identifier_token,
  4030.                            identifier_token,
  4031.                            type -> Name(),
  4032.                            file_symbol -> Name());
  4033.         }
  4034.     }
  4035.  
  4036.     return type;
  4037. }
  4038.  
  4039.  
  4040. //
  4041. //
  4042. //
  4043. TypeSymbol *Semantic::MustFindType(Ast *name)
  4044. {
  4045.     TypeSymbol *type;
  4046.     LexStream::TokenIndex identifier_token;
  4047.  
  4048.     AstSimpleName *simple_name = name -> SimpleNameCast();
  4049.     if (simple_name)
  4050.     {
  4051.         identifier_token = simple_name -> identifier_token;
  4052.         type = FindType(identifier_token);
  4053.  
  4054.         //
  4055.         // If the type was not found, try to read it again to generate an appropriate error message.
  4056.         //
  4057.         if (! type)
  4058.         {
  4059.             NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  4060.             PackageSymbol *package = (compilation_unit -> package_declaration_opt ? this_package : control.unnamed_package);
  4061.             FileSymbol *file_symbol = Control::GetFile(control, package, name_symbol);
  4062.  
  4063.             //
  4064.             // If there is no file associated with the name of the type then the type does
  4065.             // not exist. Before invoking ReadType to issue a "type not found" error message,
  4066.             // check whether or not the user is not attempting to access an inaccessible
  4067.             // private type.
  4068.             //
  4069.             if ((! file_symbol) && state_stack.Size() > 0)
  4070.             {
  4071.                 for (TypeSymbol *super_type = ThisType() -> super; super_type; super_type = super_type -> super)
  4072.                 {
  4073.                     assert(super_type -> expanded_type_table);
  4074.  
  4075.                     TypeShadowSymbol *type_shadow_symbol = super_type -> expanded_type_table -> FindTypeShadowSymbol(name_symbol);
  4076.                     if (type_shadow_symbol)
  4077.                     {
  4078.                         type = FindTypeInShadow(type_shadow_symbol, identifier_token);
  4079.                         break;
  4080.                     }
  4081.                 }
  4082.             }
  4083.  
  4084.             if (type)
  4085.                  ReportTypeInaccessible(name -> LeftToken(), name -> RightToken(), type);
  4086.             else type = ReadType(file_symbol, package, name_symbol, identifier_token);
  4087.         }
  4088.         else
  4089.         {
  4090.              TypeAccessCheck(name, type);
  4091.         }
  4092.     }
  4093.     else
  4094.     {
  4095.         AstFieldAccess *field_access = name -> FieldAccessCast();
  4096.  
  4097.         assert(field_access);
  4098.  
  4099.         identifier_token = field_access -> identifier_token;
  4100.  
  4101.         ProcessPackageOrType(field_access -> base);
  4102.         Symbol *symbol = field_access -> base -> symbol;
  4103.  
  4104.         type = symbol -> TypeCast();
  4105.         if (type)
  4106.         {
  4107.             TypeNestAccessCheck(field_access -> base);
  4108.             type = MustFindNestedType(type, field_access);
  4109.         }
  4110.         else
  4111.         {
  4112.             PackageSymbol *package = symbol -> PackageCast();
  4113.             if (package -> directory.Length() == 0)
  4114.             {
  4115.                 ReportSemError(SemanticError::PACKAGE_NOT_FOUND,
  4116.                                field_access -> base -> LeftToken(),
  4117.                                field_access -> base -> RightToken(),
  4118.                                package -> PackageName());
  4119.             }
  4120.             NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  4121.             type = package -> FindTypeSymbol(name_symbol);
  4122.             if (! type)
  4123.             {
  4124.                 FileSymbol *file_symbol = Control::GetFile(control, package, name_symbol);
  4125.                 type = ReadType(file_symbol, package, name_symbol, identifier_token);
  4126.             }
  4127.             else
  4128.             {
  4129.                 if (type -> SourcePending())
  4130.                     control.ProcessHeaders(type -> file_symbol);
  4131.                 TypeAccessCheck(name, type);
  4132.             }
  4133.         }
  4134.     }
  4135.  
  4136.     //
  4137.     // Establish a dependence from the base_type to a type that it "must find".
  4138.     // Note that the only time an environment is not available is when were are
  4139.     // processing the type header of an outermost type.
  4140.     //
  4141.     if (state_stack.Size() > 0)
  4142.         AddDependence(ThisType(), type, identifier_token);
  4143.  
  4144.     //
  4145.     //
  4146.     //
  4147.     if (control.option.deprecation && type -> IsDeprecated() && type -> file_symbol != source_file_symbol)
  4148.     {
  4149.         ReportSemError(SemanticError::DEPRECATED_TYPE,
  4150.                        name -> LeftToken(),
  4151.                        name -> RightToken(),
  4152.                        type -> ContainingPackage() -> PackageName(),
  4153.                        type -> ExternalName());
  4154.     }
  4155.  
  4156.     return type;
  4157. }
  4158.  
  4159.  
  4160. void Semantic::ProcessInterface(TypeSymbol *base_type, AstExpression *name)
  4161. {
  4162.     TypeSymbol *interf = MustFindType(name);
  4163.  
  4164.     assert(! interf -> SourcePending());
  4165.  
  4166.     if (! interf -> ACC_INTERFACE())
  4167.     {
  4168.         if (! interf -> Bad())
  4169.         {
  4170.             ReportSemError(SemanticError::NOT_AN_INTERFACE,
  4171.                            name -> LeftToken(),
  4172.                            name -> RightToken(),
  4173.                            interf -> ContainingPackage() -> PackageName(),
  4174.                            interf -> ExternalName());
  4175.         }
  4176.  
  4177.         name -> symbol = NULL;
  4178.     }
  4179.     else
  4180.     {
  4181.         for (int k = 0; k < base_type -> NumInterfaces(); k++)
  4182.         {
  4183.             if (base_type -> Interface(k) == interf)
  4184.             {
  4185.                 ReportSemError(SemanticError::DUPLICATE_INTERFACE,
  4186.                                name -> LeftToken(),
  4187.                                name -> RightToken(),
  4188.                                interf -> ContainingPackage() -> PackageName(),
  4189.                                interf -> ExternalName(),
  4190.                                base_type -> ExternalName());
  4191.  
  4192.                 name -> symbol = NULL;
  4193.  
  4194.                 return;
  4195.             }
  4196.         }
  4197.  
  4198.         name -> symbol = interf; // save type  name in ast.
  4199.         base_type -> AddInterface(interf);
  4200.     }
  4201.  
  4202.     return;
  4203. }
  4204.  
  4205.  
  4206. void Semantic::InitializeVariable(AstFieldDeclaration *field_declaration, Tuple<VariableSymbol *> &finals)
  4207. {
  4208.     ThisMethod() = NULL;
  4209.  
  4210.     for (int i = 0; i < field_declaration -> NumVariableDeclarators(); i++)
  4211.     {
  4212.         AstVariableDeclarator *variable_declarator = field_declaration -> VariableDeclarator(i);
  4213.  
  4214.         if ((ThisVariable() = variable_declarator -> symbol))
  4215.         {
  4216.             if (variable_declarator -> variable_initializer_opt)
  4217.             {
  4218.                 variable_declarator -> pending = true;
  4219.  
  4220.                 int start_num_errors = NumErrors();
  4221.  
  4222.                 ProcessVariableInitializer(variable_declarator);
  4223.                 if (NumErrors() == start_num_errors)
  4224.                      DefiniteVariableInitializer(variable_declarator, finals);
  4225.                 else variable_declarator -> symbol -> MarkDefinitelyAssigned(); // assume variable is assigned
  4226.  
  4227.                 variable_declarator -> pending = false;
  4228.             }
  4229.             ThisVariable() -> MarkComplete();
  4230.         }
  4231.     }
  4232.  
  4233.     return;
  4234. }
  4235.  
  4236.  
  4237. inline void Semantic::ProcessInitializer(AstBlock *initializer_block, AstBlock *block_body,
  4238.                                          MethodSymbol *init_method, Tuple<VariableSymbol *> &finals)
  4239. {
  4240.     ThisVariable() = NULL;
  4241.     ThisMethod() = init_method;
  4242.  
  4243.     LocalBlockStack().Push(initializer_block);
  4244.     LocalSymbolTable().Push(init_method -> block_symbol -> Table());
  4245.  
  4246.     int start_num_errors = NumErrors();
  4247.  
  4248.     AstConstructorBlock *constructor_block = block_body -> ConstructorBlockCast();
  4249.     if (constructor_block)
  4250.     {
  4251.         assert(constructor_block -> explicit_constructor_invocation_opt);
  4252.  
  4253.         ReportSemError(SemanticError::MISPLACED_EXPLICIT_CONSTRUCTOR_INVOCATION,
  4254.                        constructor_block -> explicit_constructor_invocation_opt -> LeftToken(),
  4255.                        constructor_block -> explicit_constructor_invocation_opt -> RightToken());
  4256.     }
  4257.  
  4258.     ProcessBlock(block_body);
  4259.  
  4260.     if (NumErrors() == start_num_errors)
  4261.         DefiniteBlockInitializer(block_body, LocalBlockStack().max_size, finals);
  4262.  
  4263.     //
  4264.     // If an enclosed block has a higher max_variable_index than the current block,
  4265.     // update max_variable_index in the current_block, accordingly.
  4266.     //
  4267.     if (init_method -> block_symbol -> max_variable_index < LocalBlockStack().TopMaxEnclosedVariableIndex())
  4268.         init_method -> block_symbol -> max_variable_index = LocalBlockStack().TopMaxEnclosedVariableIndex();
  4269.  
  4270.     LocalBlockStack().Pop();
  4271.     LocalSymbolTable().Pop();
  4272.  
  4273.     return;
  4274. }
  4275.  
  4276.  
  4277. bool Semantic::NeedsInitializationMethod(AstFieldDeclaration *field_declaration)
  4278. {
  4279.     //
  4280.     // We need a static constructor-initializer if we encounter at least one class
  4281.     // variable that is declared with an initializer is not a constant expression.
  4282.     //
  4283.     for (int i = 0; i < field_declaration -> NumVariableDeclarators(); i++)
  4284.     {
  4285.         AstVariableDeclarator *variable_declarator = field_declaration -> VariableDeclarator(i);
  4286.         if (variable_declarator -> variable_initializer_opt)
  4287.         {
  4288.             AstExpression *init = variable_declarator -> variable_initializer_opt -> ExpressionCast();
  4289.             if (! (init && init -> IsConstant()))
  4290.                 return true;
  4291.  
  4292.             //
  4293.             // TODO: there seems to be a contradiction between the language spec and the VM spec.
  4294.             // The language spec seems to require that a variable be initialized (in the class file)
  4295.             // with a "ConstantValue" only if it is static. The VM spec, on the other hand, states
  4296.             // that a static need not be final to be initialized with a ConstantValue.
  4297.             // As of now, we are following the language spec - ergo, this extra test.
  4298.             //
  4299.             if (variable_declarator -> symbol && (! variable_declarator -> symbol -> ACC_FINAL()))
  4300.                 return true;
  4301.         }
  4302.     }
  4303.  
  4304.     return false;
  4305. }
  4306.  
  4307.  
  4308. void Semantic::ProcessStaticInitializers(AstClassBody *class_body)
  4309. {
  4310.     if (class_body -> NumStaticInitializers() > 0 || class_body -> NumClassVariables() > 0)
  4311.     {
  4312.         TypeSymbol *this_type = ThisType();
  4313.  
  4314.         BlockSymbol *block_symbol = new BlockSymbol(0); // the symbol table associated with this block will contain no element
  4315.         block_symbol -> max_variable_index = 0;         // if we need this, it will be associated with a static function.
  4316.  
  4317.         AstBlock *initializer_block = compilation_unit -> ast_pool -> GenBlock();
  4318.         initializer_block -> left_brace_token  = class_body -> left_brace_token;
  4319.         initializer_block -> right_brace_token = class_body -> left_brace_token;
  4320.         initializer_block -> block_symbol = block_symbol;
  4321.         initializer_block -> nesting_level = LocalBlockStack().Size();
  4322.  
  4323.         LocalBlockStack().max_size = 0;
  4324.  
  4325.         //
  4326.         // If the class being processed contains any static
  4327.         // initializer then, it needs a static initializer function?
  4328.         //
  4329.         MethodSymbol *init_method = NULL;
  4330.         if (class_body -> NumStaticInitializers() > 0)
  4331.         {
  4332.               init_method = this_type -> InsertMethodSymbol(control.clinit_name_symbol);
  4333.  
  4334.               init_method -> SetType(control.void_type);
  4335.               init_method -> SetACC_FINAL();
  4336.               init_method -> SetACC_STATIC();
  4337.               init_method -> SetContainingType(this_type);
  4338.               init_method -> SetBlockSymbol(block_symbol);
  4339.               init_method -> SetSignature(control);
  4340.         }
  4341.  
  4342.         //
  4343.         // Compute the set of final variables declared by the user in this type.
  4344.         //
  4345.         Tuple<VariableSymbol *> finals(this_type -> NumVariableSymbols());
  4346.         for (int i = 0; i < this_type -> NumVariableSymbols(); i++)
  4347.         {
  4348.             VariableSymbol *variable_symbol = this_type -> VariableSym(i);
  4349.             if (variable_symbol -> ACC_FINAL())
  4350.                 finals.Next() = variable_symbol;
  4351.         }
  4352.  
  4353.         //
  4354.         // The static initializers and class variable initializers are executed in textual order... 8.5
  4355.         //
  4356.         int j,
  4357.             k;
  4358.         for (j = 0, k = 0; j < class_body -> NumClassVariables() && k < class_body -> NumStaticInitializers(); )
  4359.         {
  4360.             //
  4361.             // Note that since there cannot be any overlap in the
  4362.             // declarations, we can use either location position.
  4363.             // The RightToken of the field declaration is used because it
  4364.             // does not have to be computed (it is the terminating semicolon).
  4365.             // Similarly, the LeftToken of the static initializer is used
  4366.             // because it is the initial "static" keyword that marked the initializer.
  4367.             //
  4368.             //    if (class_body -> InstanceVariables(j) -> RightToken() < class_body -> Block(k) -> LeftToken())
  4369.             //
  4370.             if (class_body -> ClassVariable(j) -> semicolon_token < class_body -> StaticInitializer(k) -> static_token)
  4371.             {
  4372.                 InitializeVariable(class_body -> ClassVariable(j), finals);
  4373.                 j++;
  4374.             }
  4375.             else
  4376.             {
  4377.                 AstBlock *block_body = class_body -> StaticInitializer(k) -> block;
  4378.  
  4379.                 //
  4380.                 // The first block that is the body of an initializer is reachable
  4381.                 // A subsequent block is reachable iff its predecessor can complete
  4382.                 // normally
  4383.                 //
  4384.                 block_body -> is_reachable = (k == 0
  4385.                                                  ? true
  4386.                                                  : class_body -> StaticInitializer(k - 1) -> block -> can_complete_normally);
  4387.  
  4388.                 ProcessInitializer(initializer_block, block_body, init_method, finals);
  4389.                 k++;
  4390.             }
  4391.         }
  4392.         for (; j < class_body -> NumClassVariables(); j++)
  4393.             InitializeVariable(class_body -> ClassVariable(j), finals);
  4394.         for (; k < class_body -> NumStaticInitializers(); k++)
  4395.         {
  4396.             AstBlock *block_body = class_body -> StaticInitializer(k) -> block;
  4397.  
  4398.             //
  4399.             // The first block that is the body of an initializer is reachable
  4400.             // A subsequent block is reachable iff its predecessor can complete
  4401.             // normally
  4402.             //
  4403.             block_body -> is_reachable = (k == 0
  4404.                                              ? true
  4405.                                              : class_body -> StaticInitializer(k - 1) -> block -> can_complete_normally);
  4406.  
  4407.             ProcessInitializer(initializer_block, block_body, init_method, finals);
  4408.         }
  4409.  
  4410.         //
  4411.         // Check that each static final variables has been initialized by now.
  4412.         // If not, issue an error and assume it is.
  4413.         //
  4414.         for (int l = 0; l < finals.Length(); l++)
  4415.         {
  4416.             if (finals[l] -> ACC_STATIC() && (! finals[l] -> IsDefinitelyAssigned()))
  4417.             {
  4418.                 ReportSemError(SemanticError::UNINITIALIZED_STATIC_FINAL_VARIABLE,
  4419.                                finals[l] -> declarator -> LeftToken(),
  4420.                                finals[l] -> declarator -> RightToken());
  4421.                 finals[l] -> MarkDefinitelyAssigned();
  4422.             }
  4423.         }
  4424.  
  4425.         //
  4426.         // If we had not yet defined a static initializer function and
  4427.         // there exists a static variable in the class that is not initialized
  4428.         // with a constant then define one now.
  4429.         //
  4430.         if (! init_method)
  4431.         {
  4432.             //
  4433.             // Check whether or not we need a static initializer method. We need a static
  4434.             // constructor-initializer iff:
  4435.             //
  4436.             //     . we have at least one static initializer (this case is processed above)
  4437.             //     . we have at least one static variable that is not initialized with a
  4438.             //       constant expression.
  4439.             //
  4440.             for (int i = 0; i < class_body -> NumClassVariables(); i++)
  4441.             {
  4442.                 if (NeedsInitializationMethod(class_body -> ClassVariable(i)))
  4443.                 {
  4444.                     init_method = this_type -> InsertMethodSymbol(control.clinit_name_symbol);
  4445.  
  4446.                     init_method -> SetType(control.void_type);
  4447.                     init_method -> SetACC_FINAL();
  4448.                     init_method -> SetACC_STATIC();
  4449.                     init_method -> SetContainingType(this_type);
  4450.                     init_method -> SetBlockSymbol(block_symbol);
  4451.                     init_method -> SetSignature(control);
  4452.  
  4453.                     break;
  4454.                 }
  4455.             }
  4456.         }
  4457.  
  4458.         //
  4459.         // If an initialization method has been defined, update its max_block_depth.
  4460.         // Otherwise, deallocate the block symbol as nobody would be pointing to it
  4461.         // after this point.
  4462.         //
  4463.         if (init_method)
  4464.         {
  4465.              init_method -> max_block_depth = LocalBlockStack().max_size;
  4466.              this_type -> static_initializer_method = init_method;
  4467.  
  4468.              block_symbol -> CompressSpace(); // space optimization
  4469.         }
  4470.         else delete block_symbol;
  4471.  
  4472. // STG:
  4473. //        delete initializer_block;
  4474.     }
  4475.  
  4476.     return;
  4477. }
  4478.  
  4479.  
  4480. void Semantic::ProcessBlockInitializers(AstClassBody *class_body)
  4481. {
  4482.     TypeSymbol *this_type = ThisType();
  4483.  
  4484.     //
  4485.     // Compute the set of final variables declared by the user in this type.
  4486.     //
  4487.     Tuple<VariableSymbol *> finals(this_type -> NumVariableSymbols());
  4488.     for (int i = 0; i < this_type -> NumVariableSymbols(); i++)
  4489.     {
  4490.         VariableSymbol *variable_symbol = this_type -> VariableSym(i);
  4491.         if (variable_symbol -> ACC_FINAL())
  4492.             finals.Next() = variable_symbol;
  4493.     }
  4494.  
  4495.     //
  4496.     // Initialization code is executed by every constructor, just after the
  4497.     // superclass constructor is called, in textual order along with any
  4498.     // instance variable initializations.
  4499.     //
  4500.     if (class_body -> NumBlocks() == 0)
  4501.     {
  4502.         for (int j = 0; j < class_body -> NumInstanceVariables(); j++)
  4503.             InitializeVariable(class_body -> InstanceVariable(j), finals);
  4504.     }
  4505.     else
  4506.     {
  4507.         MethodSymbol *init_method = this_type -> InsertMethodSymbol(control.block_init_name_symbol);
  4508.         init_method -> SetType(control.void_type);
  4509.         init_method -> SetACC_FINAL();
  4510.         init_method -> SetACC_PRIVATE();
  4511.         init_method -> SetContainingType(this_type);
  4512.         init_method -> SetBlockSymbol(new BlockSymbol(0));  // the symbol table associated with this block will contain no element
  4513.         init_method -> block_symbol -> max_variable_index = 1;
  4514.         init_method -> SetSignature(control);
  4515.  
  4516.         //
  4517.         // Compute the set of constructors whose bodies do not start with
  4518.         // an explicit this call.
  4519.         //
  4520.         for (int i = 0; i < class_body -> NumConstructors(); i++)
  4521.         {
  4522.             MethodSymbol *method = class_body -> Constructor(i) -> constructor_symbol;
  4523.             if (method)
  4524.             {
  4525.                 AstConstructorBlock *constructor_block = class_body -> Constructor(i) -> constructor_body;
  4526.                 if (! (constructor_block -> explicit_constructor_invocation_opt &&
  4527.                        constructor_block -> explicit_constructor_invocation_opt -> ThisCallCast()))
  4528.                 init_method -> AddInitializerConstructor(method);
  4529.             }
  4530.         }
  4531.  
  4532.         this_type -> block_initializer_method = init_method;
  4533.  
  4534.         AstBlock *initializer_block = compilation_unit -> ast_pool -> GenBlock();
  4535.         initializer_block -> left_brace_token  = class_body -> left_brace_token;
  4536.         initializer_block -> right_brace_token = class_body -> left_brace_token;
  4537.         initializer_block -> block_symbol = init_method -> block_symbol;
  4538.         initializer_block -> nesting_level = LocalBlockStack().Size();
  4539.  
  4540.         LocalBlockStack().max_size = 0;
  4541.  
  4542.         int j,
  4543.             k;
  4544.         for (j = 0, k = 0; j < class_body -> NumInstanceVariables() && k < class_body -> NumBlocks(); )
  4545.         {
  4546.             //
  4547.             // Note that since there cannot be any overlap in the
  4548.             // declarations, we can use either location position.
  4549.             // The RightToken of the field declaration is used because it
  4550.             // does not have to be computed (it is the terminating semicolon).
  4551.             // Similarly, the LeftToken of the block initializer is used
  4552.             // because it is the initial "{".
  4553.             //
  4554.             //    if (class_body -> InstanceVariable(j) -> RightToken() < class_body -> Blocks(k) -> LeftToken())
  4555.             //
  4556.             if (class_body -> InstanceVariable(j) -> semicolon_token < class_body -> Block(k) -> left_brace_token)
  4557.             {
  4558.                 InitializeVariable(class_body -> InstanceVariable(j), finals);
  4559.                 j++;
  4560.             }
  4561.             else
  4562.             {
  4563.                 AstBlock *block_body = class_body -> Block(k);
  4564.  
  4565.                 //
  4566.                 // The first block that is the body of an initializer is reachable
  4567.                 // A subsequent block is reachable iff its predecessor can complete
  4568.                 // normally
  4569.                 //
  4570.                 block_body -> is_reachable = (k == 0 ? true : class_body -> Block(k - 1) -> can_complete_normally);
  4571.  
  4572.                 ProcessInitializer(initializer_block, block_body, init_method, finals);
  4573.                 k++;
  4574.             }
  4575.         }
  4576.         for (; j < class_body -> NumInstanceVariables(); j++)
  4577.             InitializeVariable(class_body -> InstanceVariable(j), finals);
  4578.         for (; k < class_body -> NumBlocks(); k++)
  4579.         {
  4580.             AstBlock *block_body = class_body -> Block(k);
  4581.  
  4582.             //
  4583.             // The first block that is the body of an initializer is reachable
  4584.             // A subsequent block is reachable iff its predecessor can complete
  4585.             // normally
  4586.             //
  4587.             block_body -> is_reachable = (k == 0 ? true : class_body -> Block(k - 1) -> can_complete_normally);
  4588.  
  4589.             ProcessInitializer(initializer_block, block_body, init_method, finals);
  4590.         }
  4591.  
  4592.         init_method -> max_block_depth = LocalBlockStack().max_size;
  4593.         init_method -> block_symbol -> CompressSpace(); // space optimization
  4594.  
  4595.         //
  4596.         // Note that unlike the case of static fields. We do not ensure here that
  4597.         // each final instance variable has been initialized at this point, because
  4598.         // the user may chose instead to initialize such a final variable in every
  4599.         // constructor instead. See body.cpp
  4600.         //
  4601.  
  4602. // STG:
  4603. //        delete initializer_block;
  4604.     }
  4605.  
  4606.     return;
  4607. }
  4608.